364,083 Members | 5932 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Compare Two DataBase

ComputerTechie
P: 6
Hello fellow Experts,

i have two database with the following tables
customers and scrub.

customers table has folowing:
id customers
Phone
First Name
Last name
Address
City
State
Zipcode
Email
Investment
Source
Assign
Statue
CustomerNotes

Scrub has the following
idscrub
First
Last

What i am trying to do is compare the scrub Database with the customer Database and find all the matchs to delete them out of customer database.

So far my code work for first record in customer them stops. I been changed code and still get the same results.

if there an easier way please let me know.

i have 3355 names to take out of the 77005 customer database.

I working with jsp and mysql.

Thanks for your time,

CT
Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.sql.*"%>
  2. <%@ page import="javax.servlet.*"%>
  3.  
  4.  
  5. <%
  6. String connectionURL = "jdbc:mysql://localhost:3306/bjc?user=;password=";
  7. Connection connection = null;
  8. Connection Mconnection = null;
  9. Statement statement = null;
  10. Statement Mstatement = null;
  11. ResultSet rs = null;
  12. ResultSet Mrs = null;
  13.  
  14.  
  15. Class.forName("com.mysql.jdbc.Driver").newInstance();
  16. Mconnection = DriverManager.getConnection(connectionURL, "root", "password");
  17. connection = DriverManager.getConnection(connectionURL, "root", "password");
  18. Mstatement = Mconnection.createStatement();
  19. statement = connection.createStatement();
  20. Mrs = Mstatement.executeQuery("SELECT * FROM scrub");
  21. rs = rs = statement.executeQuery("SELECT * FROM customers");
  22.  
  23. String holder1 = null;
  24. String holder1a = null;
  25. String holder2 = null;
  26. String holder2a = null;
  27. int idholder = 0;
  28. int temp = 0;
  29.  
  30. while (rs.next()){
  31.     idholder = rs.getInt("idcustomer");
  32.  
  33.  while (Mrs.next()){
  34.      holder1 = Mrs.getString("last");
  35.      holder2 = rs.getString("Last Name");
  36.      holder1a = Mrs.getString("First");
  37.      holder2a = rs.getString("First Name");
  38.  
  39.      out.println(idholder);
  40.      out.println(" ");
  41.      out.println(temp);
  42.      out.println(" ");
  43.      out.println(holder1a);
  44.      out.println(" ");
  45.      out.println(holder1);
  46.      out.println(" ");
  47.      out.println(holder2a);
  48.      out.println(" ");
  49.      out.println(holder2);
  50.      out.println("<br>");
  51.  
  52.  
  53.      if (holder2.equalsIgnoreCase(holder1) && holder2a.equalsIgnoreCase(holder2)){
  54.          out.println("row "+idholder+" "+holder2a+" "+holder2+"\n");
  55.      } 
  56.      } temp++;  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. rs.close();
  63. statement.close();
  64. connection.close();
  65.  
  66.  
  67. %>
  68.  
Jan 25 '12 #1

✓ answered by ComputerTechie

I cam up with the following solution.


Expand|Select|Wrap|Line Numbers
  1. SELECT customers.`First Name`,customers.`Last Name`,customers.idcustomer,scrub.idscrub,scrub.`First`,scrub.Last
  2. FROM customers ,scrub
  3. WHERE customers.`First Name` = scrub.`First` AND customers.`Last Name` = scrub.Last
  4. ORDER BY customers.idcustomer  
  5.  
  6.  
  7.  
  8. DELETE FROM customers c
  9. WHERE EXISTS (SELECT 'X' FROM scrub
  10. WHERE c.`First Name` = scrub.`First` AND c.`Last Name` = scrub.Last
  11. ORDER BY c.idcustomer )
Share this Question
Share on Google+
7 Replies


C CSR
100+
P: 144
Why does it look like you're comparing holder2a to holder2? Shouldn't it be holder2 = holder1, holder2a = holder1a?
Jan 25 '12 #2

ComputerTechie
P: 6
Sorry i did post the older code here is new line

Expand|Select|Wrap|Line Numbers
  1. if (holder2.equalsIgnoreCase(holder1) && holder2a.equalsIgnoreCase(holder1a))
  2.  
as for the compare you can not as for are i know use = and have to use .equals

Expand|Select|Wrap|Line Numbers
  1. if (holder2.equalsIgnoreCase(holder1) && holder2a.equalsIgnoreCase(holder2)){
CT
Jan 25 '12 #3

C CSR
100+
P: 144
I used "=" just as shorthand. If the New code still isn't working I'm out of my league on this. I was just looking at random questions and I noticed the 2a = 2 discrepancy. Hang in there:)
Jan 25 '12 #4

lgm001
P: 9
God you people make your lives difficult...

Reads your mySql manual...

Using SQL outer joins will yield the differences.

SELECT * FROM customer LEFT JOIN scrub
-> ON customer.firstname = scrub.firstname
-> AND customer.lastname = scrub.last
-> WHERE customer.firstname IS NULL;

Basically this says give me all the records that are in scrub and match them with customer... Make an empty customer.name field if there isn't a match in the customer... All you will get from this select is whats left in scrub.
Jan 25 '12 #5

ComputerTechie
P: 6
lgm001,

You need to test what you wrote. it does not work but i did come up with a solustion that i will post. soon.

CT
Jan 26 '12 #6

lgm001
P: 9
Well... post a dataset to work with then...
Jan 26 '12 #7

ComputerTechie
P: 6
I cam up with the following solution.


Expand|Select|Wrap|Line Numbers
  1. SELECT customers.`First Name`,customers.`Last Name`,customers.idcustomer,scrub.idscrub,scrub.`First`,scrub.Last
  2. FROM customers ,scrub
  3. WHERE customers.`First Name` = scrub.`First` AND customers.`Last Name` = scrub.Last
  4. ORDER BY customers.idcustomer  
  5.  
  6.  
  7.  
  8. DELETE FROM customers c
  9. WHERE EXISTS (SELECT 'X' FROM scrub
  10. WHERE c.`First Name` = scrub.`First` AND c.`Last Name` = scrub.Last
  11. ORDER BY c.idcustomer )
Jan 27 '12 #8

Post your reply

Help answer this question



Didn't find the answer to your Java question?

You can also browse similar questions: Java