Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1.  
  2. package myJdbc;
  3.  
  4. import java.sql.*;
  5.  
  6.  
  7. public class MyDerby {
  8.  
  9. public static void main(String[] args)
  10. {
  11. try {
  12. String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  13. //String driver = "com.mysql.jdbc.Driver";
  14. Class.forName(driver).newInstance();
  15. Connection conn = null;
  16. //conn = DriverManager.getConnection("jdbc:derby://localhost:1527/car_data;user=timo;password=timo");
  17. //conn = DriverManager.getConnection("jdbc:mysql://localhost:3301/car_database", "timo", "timo");
  18. conn = DriverManager.getConnection("jdbc:derby://localhost:1527/car_data" , "timo" , "timo");
  19.  
  20. Statement s = conn.createStatement();
  21. ResultSet rs = s.executeQuery("SELECT * FROM CAR");
  22.  
  23. while(rs.next()) {
  24. System.out.println("MAKE : "+ rs.getString(1));
  25. System.out.println("MODEL : "+ rs.getString(2));
  26. System.out.println("PRICE: "+ rs.getInt("PRICE"));
  27. System.out.println();
  28. }
  29. rs.close();
  30.  
  31. //s.executeUpdate("INSERT INTO CAR (make, model) values('eka', 'toka')");
  32. int rows = s.executeUpdate("UPDATE CAR SET price=10000 where make = 'Buick'");
  33.  
  34. System.out.println("rows " + rows);
  35.  
  36.  
  37. String strSql = "UPDATE CAR SET price=?, make=? where model = 'A8'";
  38.  
  39. PreparedStatement preStmt = conn.prepareStatement(strSql);
  40.  
  41. preStmt.setInt(1, 300);
  42. preStmt.setString(2, "Audi");
  43. preStmt.executeUpdate();
  44.  
  45.  
  46. s.close();
  47. conn.close();
  48. } catch(Exception e) {
  49. System.out.println("Exception: "+ e);
  50. e.printStackTrace();
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement