Advertisement
Guest User

Untitled

a guest
Nov 30th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. Jdbc dbConn = new Jdbc();
  2. Connection con = dbConn.connect();
  3. String insert_member = "INSERT INTO Members (id, name, address, dob, "
  4. + "dor, status, balance)" + " VALUES (?, ?, ?, ?, ?, ?, ?)";
  5. //String c = con.toString();
  6. //con = null for some reason
  7. pstmt = con.prepareStatement(insert_member);
  8. pstmt.setString(1, "coo");
  9. pstmt.setString(2, "aa");
  10. pstmt.setString(3, "26 road");
  11. pstmt.setDate(4, new java.sql.Date(new Date().getTime()));
  12. pstmt.setDate(5, new java.sql.Date(new Date().getTime()));
  13. pstmt.setString(6, "APPLIED");
  14. pstmt.setFloat(7, 10);
  15. pstmt.execute();
  16.  
  17. public Jdbc() {
  18. String db = "xyz_assoc";
  19. try {
  20. Class.forName("com.mysql.jdbc.Driver");
  21. connection = DriverManager.getConnection
  22. ("jdbc:mysql://localhost:3306/"+db.trim(), "root", "");
  23. }
  24. catch(ClassNotFoundException cfe){
  25.  
  26. }
  27. //SQL exception
  28. catch (SQLException ex) {
  29. Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
  30. }
  31. }
  32.  
  33. //open connection to database
  34. public Connection connect(){
  35. return connection;
  36. }
  37.  
  38. //execute mysql queryS
  39. public ResultSet executeQuery(String sql_query) throws SQLException {
  40. statement = null;
  41. statement = connection.createStatement();
  42. rs = statement.executeQuery(sql_query);
  43. return rs;
  44. }
  45.  
  46. //close connection to database
  47. //destroy any remaining objects
  48. public void close() throws SQLException{
  49. statement.close();
  50. rs.close();
  51. connection.close();
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement