Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by 范思宇 on 2017/6/26.
- */
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- ///
- ///单列连接数据库
- ///
- public class dbConnect {
- public static final String url = "jdbc:mysql://127.0.0.1/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true";
- public static final String name = "com.mysql.cj.jdbc.Driver";
- public static final String user = "root";
- public static final String password = "123456";
- private static dbConnect instance;
- public Connection conn=null;
- public static dbConnect getInstance() {
- if(instance==null)
- {
- instance=new dbConnect(url);
- }
- return instance;
- }
- /**
- @param url 传入数据库地址
- */
- public dbConnect(String url)
- {
- try
- {
- Class.forName(name);
- conn=DriverManager.getConnection(url,user,password);
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- ///
- ///关闭数据库连接
- ///
- public void close()
- {
- try{
- this.conn.close();;
- }catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement