目录
类加载器
创建驱动对象
程序运行时
Driver+java.util.Properties
DriverManager获取连接的三种方式
连接DB的封装
博文地址:https://blog.csdn.net/HYZX_9987,欢迎访问。
纵使方式有多种,但步骤大致一样,必要说明都在注释中。
package com.wl.conn;
import java.sql.Connection;
import java.sql.DriverManager;
/*保证服务启动
* OracleServiceXE:保证数据库可登录
* OracleXETNSListenser:保证jdbc可连接
* 1、注册驱动/加载驱动
* 2、获得数据库连接
* 3、创建statement对象
* 4、执行sql语句
* 5、处理结果集(如果有的话,select语句会有结果集)
* 6、关闭资源
*/
public class Demo2 {
public static void main(String[] args){
/*
* 确定连接什么类型的数据库,java.sql.Driver接口的实现类;需要写入接口实现类的全限定名
* 这种方式最常用
*/
String driver = "oracle.jdbc.driver.OracleDriver";
/*
* 两种方式:jdbc:oracle:thin:@127.0.0.1:1521:XE
* jdbc:oracle:oci:@127.0.0.1:1521:XE
*/
//确定连接哪台电脑上的数据库 192.168.2.17 127.0.0.1
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
String user="wang";
String password = "wang"; //连接数据库所使用的用户名和密码
Connection connection = null;
try {
//1、注册驱动
Class.forName(driver);
//2、获得数据库连接
connection = DriverManager.getConnection(url, user, password);
System.out.println(connection); //打印出来是地址值,连接成功
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.wl.conn;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import oracle.jdbc.driver.OracleDriver;
public class Demo22 {
public static void main(String[] args){
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
String user="wang";
String password = "wang"; //连接数据库所使用的用户名和密码
Connection connection = null;
try {
//1、创建驱动对象
Driver driver = new OracleDriver();
//注册驱动:要使驱动管理器管理驱动,就得将驱动注册进来
DriverManager.registerDriver(driver);
//2、获得数据库连接
connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.wl.conn;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import oracle.jdbc.driver.OracleDriver;
public class Demo23_2 {
public static void main(String[] args){
/*
* 加载驱动的方式2.2:
* jdbc.drivers.property
*/
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
String user="wang";
String password = "wang"; //连接数据库所使用的用户名和密码
Connection connection = null;
try {
//1、注册驱动
System.setProperty("jdbc.drivers", "oracle.jdbc.driver.OracleDriver");
//2、获得数据库连接,DriverManager根据key找到value,名字是固定的
connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.wl.conn;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
import oracle.jdbc.driver.OracleDriver;
/*
* 使用driver连接,不一定必须使用DriverManager
* 真正连接的是Driver
*/
public class Demo3 {
public static void main(String[] args){
String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
Connection connection = null;
try {
//1、创建驱动对象
Driver driver = new OracleDriver();
//2、获得数据库连接
Properties info = new Properties();
info.setProperty("user", "wang");
info.setProperty("password", "wang");
connection = driver.connect(url, info);
System.out.println(connection);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//2、获得数据库连接;3种方法
//DriverManager.getConnection(url); 有些数据库直接在后面可以加 user=?等
//DriverManager.getConnection(url, info); info是Properties类型的
//DriverManager.getConnection(url, user, password);
上述的几种连接数据库的方式都对,但是我们项目中不可能每使用一次就写一次连接的代码,这样台麻烦且没有意义,因此进行封装。该方法需要读取配置文件。
配置文件名:dbInfo.properties,与代码同级即可(路径自定义,只需对应修正加载文件的路径)
内容:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
user=wang
password=wang
代码如下:
package com.wl.template;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/*
* 提供方法获取数据库连接
*/
public class DBUtils {
public static void main(String[] args) throws Exception {
getConnection();
System.out.println(getConnection());
}
private static Properties properties = new Properties();
private static String driver="oracle.jdbc.driver.OracleDriver";
private static String url;
private static String user;
private static String password;
private static InputStream in ;
static{
//将资源转为输入流
try {
// in = DBUtils.class.getResourceAsStream("src/com/wl/test2/dbInfo.properties");
// in = DBUtils.class.getClassLoader().getResourceAsStream("dbInfo.properties");
// System.out.println(in);
properties.load(DBUtils.class.getResourceAsStream("dbInfo.properties"));
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
System.out.println(url+"--"+user+"---"+password);
} catch (IOException e) {
throw new RuntimeException("加载数据库配置资源失败,运行时异常",e);
// e.printStackTrace();
}
}
public static Connection getConnection()throws Exception{
Class.forName(driver);
return DriverManager.getConnection(url, user, password);
}
public static void closeResource(ResultSet resultSet,Statement statement,Connection connection){
if(resultSet!=null)
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(statement!=null)
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(connection!=null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
封装后怎么使用它?
package com.wl.template;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
public class DBUtileTest {
Connection connection = null;
Statement statement = null ;
ResultSet resultSet = null;
public void select(int id){
try {
connection = DBUtils.getConnection(); //连接
statement = connection.createStatement();
// String sql = "select * from student where name='"+name+"'";
String sql = "select * from student where id="+id+"";
resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
long sid = resultSet.getLong(1); // 从id这一列拿值,number转为long类型
String sname = resultSet.getString(2); // varchar转为String类型
String city = resultSet.getString(3);
Date time = resultSet.getDate(4);
System.out.println("学号:" + sid + ",姓名:" + sname + ",家乡:" + city + ",时间:" + time);
System.out.println("-------------------------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtils.closeResource(resultSet, statement, connection);
}
}
public static void main(String[] args) {
// new DBUtileTest().select("1416270244");
DBUtileTest t = new DBUtileTest();
t.select(1416270244);
}
}