LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-28-2014, 03:28 AM   #1
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Rep: Reputation: 12
Problem to copy values from one table to another in Mysql


Hello,
I am attempting to copy values from one table to another by eliminating the duplicate values. The logic behind the process is, "Insert the first row in the destination table. If the succeeding rows of the source table are same as in the destination, do not insert. Otherwise, insert". This is the code.
Code:
//removing duplicates from the database
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
public class duplicate
{
	public static void main(String args[])
	{
		Statement stat=null,stat1=null,stat2=null;
		PreparedStatement pstat;
		Connection con=null;
		ResultSet result1,result2;
		int s_id=0;
		String date=null,time=null,temp=null;
		try
		{
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("Loaded Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/preprocessor","root","bigdata");
			System.out.println("Connected to mysql");
			stat=con.createStatement();
			stat1=con.createStatement();
			stat2=con.createStatement();
			String query1="insert into temperature_demo(ddslno,ddtstation_id,ddtdate,ddttime,ddtemp,ddtype_code,ddelevation) select dslno,dtstation_id,dtdate,dttime,dtemp,dtype_code,delevation from temperature_dup where dslno=1";
			String query2="select * from temperature_demo";
			String query3="select * from temperature_dup";
			//stat1.execute(query1);
			result1=stat1.executeQuery(query2);
			result2=stat2.executeQuery(query3);

				while(result1.next())
				{
					s_id=result1.getInt(2);
					date=result1.getString(3);
					time=result1.getString(4);
					temp=result1.getString(5);
					break;
				}
				//String query4=;
				
				pstat=con.prepareStatement("INSERT INTO temperature_demo VALUES(?,?,?,?,?,?,?)");
				while(result2.next())
				{
					int sno=result2.getInt(1);
					int s1_id=result2.getInt(2);
					String date1=result2.getString(3);
					String time1=result2.getString(4);
					String temp1=result2.getString(5);
					String type_cd=result2.getString(6);
					String elev=result2.getString(7);
					
					try
					{
					if( (s_id==s1_id)&&(date.equals(date1))&&(time.equals(time1))&&(temp.equals(temp1)) )
						;
					else
					{
						System.out.println(sno+" "+s1_id+" "+date1);
						pstat.setInt(1,sno);
						pstat.setInt(2,s1_id);
						pstat.setString(3,date1);
						pstat.setString(4,time1);
						pstat.setString(5,temp1);
						pstat.setString(6,type_cd);
						pstat.setString(7,elev);
					}
					}
					catch(Exception ex)
					{

					}
				}
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		finally
		{
			if(con!=null)
				try
				{
					con.close();
				}
				catch(Exception ex)
				{

				}
			if(stat1!=null)
				try
				{
					stat1.close();
				}
				catch(Exception ex)
				{

				}
			if(stat2!=null)
				try
				{
					stat2.close();
				}
				catch(Exception ex)
				{

				}
		}
	}
}
The problem is, The values being inserted using PreparedStatement is not being reflected in the table. Kindly Help. Thanks
 
Old 03-28-2014, 09:15 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,608

Rep: Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960Reputation: 7960
Quote:
Originally Posted by abhishekgit View Post
Hello,
I am attempting to copy values from one table to another by eliminating the duplicate values. The logic behind the process is, "Insert the first row in the destination table. If the succeeding rows of the source table are same as in the destination, do not insert. Otherwise, insert". This is the code.

The problem is, The values being inserted using PreparedStatement is not being reflected in the table. Kindly Help. Thanks
I think you're doing this the wrong way. You don't give us any examples of the data, database, or what you're trying to do, but from what little you've told us, it seems like you only want records, based on some unique/distinct value IN that record. Without knowing the input or output desired, or seeing all the code, there's no way we can test what you've posted.

Which takes us back to a question you asked before, and the documentation you were pointed to before:
http://www.linuxquestions.org/questi...se-4175494534/
https://dev.mysql.com/doc/refman/5.7/en/select.html

Reading the documentation on the select command will show you the "DISTINCT" flag...which will let you select just unique records. Once you have those, you will have a subset that you can then insert into the other database. Or, you can delete the duplicates from the input database first, then do your migration.
 
Old 03-28-2014, 09:20 AM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
I didn't look at your code. But you mention that you are using Mysql. And your requirements are:
  • copy values from one table to another by eliminating the duplicate values

In mysql, with the use of a temporary tables this is possible without a bunch of java code by doing this:

Code:
SELECT DISTINCT * INTO temporary_table FROM actual_table;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I copy user credentials from a MySQL table into SSH users Zoner Linux - Newbie 3 03-13-2011 08:25 PM
mysql+php - table colum with multiple values? i need logic help. blancs Programming 1 12-01-2008 02:08 AM
mysql with a problem table nephish Linux - Server 2 07-24-2008 12:19 AM
PHP/MYSQL problem. Rename of table-name solves the problem... mosedrengen Linux - Software 3 01-06-2006 07:07 AM
Problem with mysql table name mitrship Programming 2 07-25-2005 07:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration