package com.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
public class JDBCtest01_DriverManager {
// public static void main(String[] args) throws Exception {
/*//简单试验 DriverManager()方法获取数据
* DriverManager() Class.forName("com.mysql.jdbc.Driver");
* java.sql.Connection conn=
* DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root",
* "cchaos"); System.out.println(conn);
*/
//调用通用方法
// }
//com.mysql.jdbc.Connection
public Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
//通用方法
//1,获取可变信息
String DriverClass=null;
String Url=null;
String Pwd=null;
String User=null;
InputStream in=getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(in);
/* //jdbc.properties
driver=com.mysql.jdbc.Driver
jdbcURL=jdbc:mysql://localhost:3306/testjdbc
user=root
password=cchaos */
DriverClass=properties.getProperty("driver");
Url=properties.getProperty("jdbcURL");
Pwd=properties.getProperty("password");
User=properties.getProperty("user");
//2,链接驱动
Class.forName(DriverClass);
//3,获取Connection对象
Connection conn=
(Connection) DriverManager.getConnection(Url,Pwd,User);
System.out.println(conn);
return conn;
}
}
JDBC中DriverManager方法链接数据库
最新推荐文章于 2024-08-28 16:11:28 发布