Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.Scanner;
  4.  
  5. class JDBC1
  6. {
  7. public static void main( String[] args )
  8. throws Exception
  9. {
  10. Connection conn;
  11. try
  12. {
  13.  
  14. Class.forName("org.postgresql.Driver"); // fyrir PostgreSQL
  15. java.util.Properties props = new java.util.Properties();
  16. props.setProperty("user",args[0]);
  17. props.setProperty("password",args[1]);
  18. conn = DriverManager.getConnection("jdbc:postgresql:COMPANY",props);
  19. }
  20. catch( Exception e )
  21. {
  22. try
  23. {
  24. Class.forName("org.sqlite.JDBC");
  25. //Finnum company
  26. conn = DriverManager.getConnection("jdbc:sqlite:/Users/ColdFusion/Desktop/company.db");
  27. }
  28. catch( Exception e2 )
  29. {
  30.  
  31. conn = DriverManager.getConnection("jdbc:odbc:COMPANY");
  32.  
  33. }
  34. }
  35. // (B)
  36. Statement statement = conn.createStatement();
  37. statement.executeUpdate("UPDATE EMPLOYEE SET salary = salary + 1000");
  38.  
  39. String stmt1 = "select Salary from EMPLOYEE ";
  40. PreparedStatement p = conn.prepareStatement(stmt1);
  41.  
  42. ResultSet r = p.executeQuery();
  43. double salary = 0;
  44. while( r.next() )
  45. {
  46. salary = salary + r.getDouble(1);
  47. }
  48. System.out.println(salary);
  49. String ssnum;
  50. String stmt2 = "select ssn from EMPLOYEE";
  51. PreparedStatement p1 = conn.prepareStatement(stmt2);
  52. ResultSet r1 = p1.executeQuery();
  53. while( r1.next() )
  54. {
  55. ssnum = r1.getString(1);
  56. String stmt3 = "UPDATE EMPLOYEE SET salary = salary + 1000 WHERE ssn = ?";
  57. PreparedStatement p2 = conn.prepareStatement(stmt3);
  58. p2.setString(1,ssnum);
  59. p2.executeUpdate();
  60. }
  61. r.close();
  62. conn.close();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement