Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. //package config;
  2. //
  3. //import java.sql.Connection;
  4. //import java.sql.DriverManager;
  5. //
  6. //public class DB {
  7. // final String hostname = "localhost";
  8. // final String database = "room";
  9. // final String username = "root";
  10. // final String password = "mysql";
  11. // final String url = "jdbc:mysql://"+hostname+"/"+database+"?characterEncoding=UTF-8";
  12. // final String driver = "com.mysql.jdbc.Driver";
  13. //
  14. // public Connection getConnection(){
  15. // try {
  16. // Class.forName("com.mysql.jdbc.Driver");
  17. // return DriverManager.getConnection(this.url, this.username, this.password);
  18. // } catch (Exception e) {
  19. // e.printStackTrace();
  20. // }
  21. // return null;
  22. // }
  23. //
  24. //}// END CLASS
  25.  
  26.  
  27.  
  28.  
  29. /*
  30. * To change this license header, choose License Headers in Project Properties.
  31. * To change this template file, choose Tools | Templates
  32. * and open the template in the editor.
  33. */
  34. //package config;
  35. //
  36. //import com.mysql.jdbc.Connection;
  37. //
  38. //import controller.Main;
  39. //
  40. //import java.sql.*;
  41. //import java.util.*;
  42. //
  43. //
  44. //public class DB {
  45. // public Connection getConnection(){
  46. // try {
  47. // Class.forName("com.mysql.jdbc.Driver");
  48. // String user = "root";
  49. // String pass = "mysql";
  50. // String url = "jdbc:mysql://localhost:3306/MemberTT&T?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
  51. // return (Connection) DriverManager.getConnection(url, user, pass);
  52. // } catch (Exception e) {
  53. // e.printStackTrace();
  54. // }
  55. //
  56. // return null;
  57. // }
  58. //
  59. //
  60. //
  61. //}
  62.  
  63.  
  64. package config;
  65.  
  66. import java.sql.Connection;
  67. import java.sql.DriverManager;
  68.  
  69. public class DB {
  70.  
  71. public static void main(String[] args) throws Exception {
  72. // TODO Auto-generated method stub
  73.  
  74. getConnection();
  75.  
  76. }
  77.  
  78.  
  79. public static Connection getConnection() throws Exception{
  80.  
  81. try {
  82.  
  83.  
  84. String driver = "com.mysql.jdbc.Driver";
  85.  
  86.  
  87. String url = "jdbc:mysql://127.0.0.1:3306/room";
  88. String username = "root";
  89. String password = "mysql";
  90.  
  91.  
  92. Class.forName(driver);
  93.  
  94. Connection conn = DriverManager.getConnection(url, username, password);
  95.  
  96. System.out.println("connection success");
  97.  
  98. return conn;
  99. } catch (Exception e) {
  100. // TODO: handle exception
  101.  
  102. System.out.println(e);
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. return null;
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement