日期:2014-05-20 浏览次数:20803 次
package book.database; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnector { public static Connection getConnection (String driverClassName,String dbURL,String userName,String password) throws ClassNotFoundException,SQLException{ Connection con=null; Class.forName(driverClassName); con=DriverManager.getConnection(dbURL,userName,password); return con; } public static Connection getMYSQLConnection(String driverClassName,String serverHost,String serverPort,String dbName,String userName,String password) throws ClassNotFoundException,SQLException{ if (driverClassName==null){ driverClassName="com.mysql.jdbc.Driver"; } if(serverHost==null){ serverHost="127.0.0.1"; } if(serverPort==null){ serverPort="3306"; } String dbURL="jdbc:mysql://"+serverHost+":"+serverPort+"/"+dbName; return getConnection(driverClassName,dbURL,userName,password); } public static void main(String[] args) throws ClassNotFoundException,SQLException{ String mySQLDirver ="com.mysql.jdbc.Driver"; String dbName="books"; String userName="root"; String password="123456"; Connection con=DBConnector.getMYSQLConnection(mySQLDirver,null,null,dbName,userName,password); System.out.println("连接mysql数据库成功!"); con.close(); System.out.println("成功关闭与mysql数据库的连接!"); String url="jdbc:mysql://127.0.0.1:3306/"+dbName; con=DBConnector.getConnection(mySQLDirver,url,userName,password); System.out.println("连接mysql数据库成功!"); con.close(); System.out.println("成功关闭与mysql数据库的连接"); } }
package com.student.test3; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnector { public static Connection getConnection(String driverClassName, String dbURL, String userName, String password) throws ClassNotFoundException, SQLException { Connection con = null; Class.forName(driverClassName); con = DriverManager.getConnection(dbURL, userName, password); return con; } public static Connection getMYSQLConnection(String driverClassName, String serverHost, String serverPort, String dbName, String userName, String password) throws ClassNotFoundException, SQLException { if (driverClassName == null) { driverClassName = "com.mysql.jdbc.Driver"; } if (serverHost == null) { serverHost = "127.0.0.1"; } if (serverPort == null) { serverPort = "3306"; } String dbURL = "jdbc:mysql://" + serverHost + ":" + serverPort + "/" + dbName; return getConnection(driverClassName, dbURL, userName, password); } public static void main(String[] args) throws ClassNotFoundException, SQLException { String mySQLDirver = "com.mysql.jdbc.Driver";//这里的驱动包里面一定要有这个Driver String dbName = "study_java"; String userName = "root"; String password = "mysqladmin"; Connection con = DBConnector.getMYSQLConnection(mySQLDirver, null, null, dbName, userName, password); System.out.println("连接mysql数据库成功!"); con.close(); System.out.println("成功关闭与mysql数据库的连接!"); String url = "jdbc:mysql://127.0.0.1:3306/" + dbName; con = DBConnector.getConnection(mySQLDirver, url, userName, password); System.out.println("连接mysql数据库成功!"); con.close(); System.out.println("成功关闭与mysql数据库的连接"); } }