Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. try{
  2. Class.forName("com.mysql.cj.jdbc.Driver");
  3.  
  4. String USERNAME = "root";
  5. String PASSWORD = "root";
  6. String CONN_STRING = "jdbc:mysql://localhost:3306/javaddy?serverTimezone=UTC";
  7. Connection connection = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
  8.  
  9. try{
  10. int incrementID = increment();
  11. String pass = in;
  12. System.out.println(pass);
  13. PreparedStatement pstmt = connection.prepareStatement("INSERT INTO inquiries (LearningMat_ID,Inquiry_ID,Inquiry_question,Inquiry_answer)VALUES (?,?,?,?)");
  14. pstmt.setInt(1,0);
  15. pstmt.setInt(2, incrementID);
  16. pstmt.setString(3, pass);
  17. pstmt.setString(4, null);
  18. pstmt.execute();
  19. //
  20. bot("Your query has been forwarded to the database. An admin will answer it later.");
  21. connection.close();
  22. }catch(SQLException e){
  23. System.out.println(e);
  24. }
  25. }catch(ClassNotFoundException | SQLException e){
  26. System.out.println(e);
  27. }
  28. }
  29. public int increment() throws SQLException
  30. {
  31. int id = 1;
  32. Statement stmt = null;
  33. ResultSet rs = null;
  34.  
  35. try{
  36. Class.forName("com.mysql.cj.jdbc.Driver");
  37.  
  38. String USERNAME = "root";
  39. String PASSWORD = "root";
  40. String CONN_STRING = "jdbc:mysql://localhost:3306/javaddy?serverTimezone=UTC";
  41. Connection connection = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
  42.  
  43. try{
  44. String query = "SELECT MAX(Inquiry_id) from inquiries";
  45. stmt = connection.createStatement();
  46. rs = stmt.executeQuery(query);
  47. while(rs.next())
  48. {
  49. id = rs.getInt(1) +1;
  50. }
  51. }catch(SQLException e){
  52. System.out.println(e);
  53. }finally{
  54. connection.close();
  55. }
  56. }catch(ClassNotFoundException | SQLException e){
  57. System.out.println(e);
  58. }
  59. return id;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement