Guest User

Untitled

a guest
Jul 30th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. Java program will not connect to mySQL database
  2. public class Version {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. Connection con = null;
  7. Statement st = null;
  8. ResultSet rs = null;
  9.  
  10. //String url = "jdbc:mysql://127.0.0.1/sakila";
  11. String url = "jdbc:mysql://localhost:3306/sakila";
  12. String user = "root";
  13. String password = "root";
  14.  
  15. try {
  16.  
  17. Class.forName("com.mysql.jdbc.Driver").newInstance();
  18.  
  19. con = DriverManager.getConnection(url, user, password);
  20. st = con.createStatement();
  21. rs = st.executeQuery("select * from ACTOR;");
  22.  
  23. System.out.println("test");
  24.  
  25. if (rs.next()) {
  26. System.out.println(rs.getString(1));
  27. }
  28.  
  29. } catch (SQLException ex) {
  30. ex.printStackTrace();
  31.  
  32. } finally {
  33. try {
  34. if (rs != null) {
  35. rs.close();
  36. }
  37. if (st != null) {
  38. st.close();
  39. }
  40. if (con != null) {
  41. con.close();
  42. }
  43.  
  44. } catch (SQLException ex) {
  45. ex.printStackTrace();
  46. }
  47. }
  48. }
  49.  
  50. Class.forName("org.mysql.Driver");
  51.  
  52. Class.forName("com.mysql.jdbc.Driver").newInstance();
Add Comment
Please, Sign In to add comment