Guest User

Untitled

a guest
Sep 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. JDBC Connector Issues
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class Main {
  9. public static void main(String args[]) {
  10. // Database credentials
  11. Connection conn = null;
  12. String url = "jdbc:mysql://localhost/";
  13. String db = "db";
  14. String driver = "com.mysql.jdbc.Driver";
  15. // Connect to database
  16. try {
  17. Class.forName(driver);
  18. System.out.println("HELLO");
  19. conn = DriverManager.getConnection(url+db,"root","");
  20. System.out.println("Success");
  21. } catch(Exception e) {
  22. return;
  23. }
  24. }
  25.  
  26. }
  27.  
  28. >java -cp .:./lib/driver.jar Main
  29.  
  30. public class DBDiary {
  31.  
  32. Connection conn;
  33.  
  34.  
  35. public DBDiary(){
  36.  
  37. this.getConn();
  38.  
  39. }
  40.  
  41. public Connection getConn(){
  42.  
  43. try {
  44. conn = getConnection();
  45.  
  46. } catch (SQLException e) {
  47.  
  48. System.out.println("Connection Couldnt be Obtained");
  49. }
  50. return conn;
  51. }
  52.  
  53.  
  54. public static Connection getConnection() throws SQLException {
  55.  
  56. String drivers = "com.mysql.jdbc.Driver";
  57. String url = "jdbc:mysql://localhost:3306/test";
  58. String username = "root";
  59. String password = "root";
  60.  
  61. System.setProperty(drivers,"");
  62.  
  63. return DriverManager.getConnection(url,username,password);
  64.  
  65. }
  66.  
  67. }
Add Comment
Please, Sign In to add comment