Guest User

Untitled

a guest
Oct 16th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.util.Properties;
  5. import java.sql.*;
  6.  
  7. public class Register {
  8. public static void main(String[] args) {
  9.  
  10.  
  11. Connection con = null;
  12. String url = "jdbc:mysql://localhost:3306/";
  13. String db = "studentdb";
  14. String driver = "com.mysql.jdbc.Driver";
  15. String user = "root";
  16. String pass = "root";
  17. try {
  18.  
  19. //conn3 = DriverManager.getConnection(url3, info);
  20.  
  21. Class.forName(driver);
  22. con = DriverManager.getConnection(url+db, user, pass);
  23. if (con != null) {
  24. System.out.println("Connected to the database studentdb");
  25. System.out.println("Con-> "+con);
  26. try{
  27. //con.setAutoCommit(false);
  28. // the mysql insert statement
  29. String queryInsert = " insert into student (name, email, pass)"
  30. + " values (?, ?, ?)";
  31.  
  32.  
  33.  
  34. // execute the preparedstatement
  35. //preparedStmt.execute();
  36.  
  37. //con.commit();
  38. //con.setAutoCommit(true);
  39.  
  40.  
  41. // our SQL SELECT query.
  42. // if you only need a few columns, specify them by name instead of using "*"
  43. String query = "SELECT * FROM student";
  44. // create the java statement
  45. PreparedStatement st = con.prepareStatement(query);
  46. System.out.println("St-> "+st);
  47. // execute the query, and get a java resultset
  48. ResultSet rs = st.executeQuery();
  49. //System.out.println("-> "+rs.next());
  50. // iterate through the java resultset
  51. while (rs.next())
  52. {
  53. String name = rs.getString("name");
  54. String email = rs.getString("email");
  55. String password = rs.getString("pass");
  56.  
  57. // print the results
  58. System.out.format("%s, %s, %sn", name, email, password);
  59. }
  60. st.close();
  61. }
  62. catch(Exception e){
  63. System.out.println("error came-->"+e.getMessage());
  64. }
  65. }
  66. } catch (Exception ex) {
  67. System.out.println("An error occurred. Maybe user/password is invalid");
  68. ex.printStackTrace();
  69. }
  70. }
  71. }`enter code here`
Add Comment
Please, Sign In to add comment