Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. /**
  2. * Created by 范思宇 on 2017/6/26.
  3. */
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. ///
  9. ///单列连接数据库
  10. ///
  11. public class dbConnect {
  12. public static final String url = "jdbc:mysql://127.0.0.1/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true";
  13. public static final String name = "com.mysql.cj.jdbc.Driver";
  14. public static final String user = "root";
  15. public static final String password = "123456";
  16. private static dbConnect instance;
  17. public Connection conn=null;
  18. public static dbConnect getInstance() {
  19. if(instance==null)
  20. {
  21. instance=new dbConnect(url);
  22. }
  23. return instance;
  24. }
  25.  
  26. /**
  27. @param url 传入数据库地址
  28. */
  29. public dbConnect(String url)
  30. {
  31. try
  32. {
  33. Class.forName(name);
  34. conn=DriverManager.getConnection(url,user,password);
  35. }catch (Exception e)
  36. {
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41. ///
  42. ///关闭数据库连接
  43. ///
  44. public void close()
  45. {
  46. try{
  47. this.conn.close();;
  48. }catch (SQLException e)
  49. {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement