链接mysql数据库固定格式

package com.mysql;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
		String driver = "com.mysql.jdbc.Driver";//驱动使用mysql驱动
	    String url= "jdbc:mysql://localhost:3306/mydatabase01";
	    String user = "root";
	    String password = "root";
	    
	    public Connection conn;//建立一个链接

	    public DBConnection() {

	        try {
	            Class.forName(driver);// 加载驱动
	            conn = (Connection) DriverManager.getConnection(url, user, password);// 杩炵画鏁版嵁搴�
	            
	            if(!conn.isClosed())
	                System.out.println("Succeeded connecting to the Database!"); 
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	    
	    public void close() {
	        try {
	            this.conn.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42630877/article/details/81217178