Unit01: JDBC原理 、 JDBC基础编程
这个文件里面有两块内容:
1.用比较麻烦的方式连接数据库,test1(),test4()
2.创建DBTool,测试DBTool连接数据库 ,test5(),test6(),
注意:这个案例中没有使用连接池。
package jdbc; import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; import org.junit.Test; import util.DBTool; /**
* Junit测试类,每个方法都能单独执行。
* 1.方法前加上@Test
* 2.方法是公有的,无返回值,无参
*/
public class TestDay01 { /**
* 1.创建连接
* 2.执行DML
*/
@Test
public void test1() {
System.out.println(1);
Connection conn = null;
try {
//1.加载驱动:告诉DriverManager
//我们想要使用哪个驱动类.
Class.forName(
"oracle.jdbc.driver.OracleDriver");
//2.调用DriverManager创建连接
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.201.227:1521:orcl",
"openlab", "open123");
System.out.println(conn);
//3.创建Statement
Statement smt = conn.createStatement();
//4.执行DML
//在JDBC中写SQL不能写结束符(;)
String sql =
"insert into emps_lhh values("
+ "emps_seq_lhh.nextval,"
+ "'悟空','职员',0,sysdate,"
+ "5000.0,1000.0,2)";
//返回增加(修改/删除)的行数
int rows = smt.executeUpdate(sql);
System.out.println(rows);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 使用Properties读取db.peoperties
* 1.peoperties文件存储的是key-value
* 2.Properties类本质上就是Map,
* 专门用来读取properties文件.
*/
@Test
public void test4() {
Properties p = new Properties();
//1.获取任意类的类加载器
//2.类加载器从classes目录下读取文件
//3.db.properties刚好被编译到classes下
try {
p.load(TestDay01.class.getClassLoader()
.getResourceAsStream("db.properties"));
String driver = p.getProperty("driver");
System.out.println(driver);
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 1.测试DBTool
* 2.执行update语句
*/
@Test
public void test5() {
Connection conn = null;
try {
conn = DBTool.getConnection();
System.out.println(conn);
Statement smt = conn.createStatement();
String sql =
"update emps_lhh "
+ "set ename='刘备',"
+ "job='董事长',"
+ "sal=30000.0,"
+ "comm=90000.0 "
+ "where empno=1";
int rows = smt.executeUpdate(sql);
System.out.println(rows);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBTool.close(conn);
}
} /**
* 执行DQL语句
*/
@Test
public void test6() {
Connection conn = null;
try {
conn = DBTool.getConnection();
Statement smt = conn.createStatement();
String sql =
"select * from emps_lhh "
+ "where job='市场'";
//查询方法返回的是结果集对象,
//该对象中封装了数据库所返回的
//多行多列的数据.
ResultSet rs = smt.executeQuery(sql);
//结果集中有一个指针(变量),
//默认指向第一行之上(空值).
while(rs.next()) {
//每次next()会让指针向下移动一行,
//即变量+1.若该行有数据则返回true,
//否则返回false.
//从结果集某一行中取列的值:
//rs.get类型(字段名)
//rs.get类型(字段序号)
System.out.println(
rs.getInt("empno"));
System.out.println(
rs.getString("ename"));
System.out.println(
rs.getDouble("sal"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBTool.close(conn);
}
} }
DBTool.java文件,封装了连接数据库的功能
package util; import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties; public class DBTool { private static String driver;
private static String url;
private static String user;
private static String pwd; static {
//在类加载时只读取一次配置文件
Properties p = new Properties();
try {
p.load(DBTool.class.getClassLoader()
.getResourceAsStream("db.properties"));
driver = p.getProperty("driver");
url = p.getProperty("url");
user = p.getProperty("user");
pwd = p.getProperty("pwd");
//只需要加载一次驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(
"加载配置文件失败", e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(
"加载驱动类失败", e);
}
} /**
* 此处直接抛出SQLException,是为了强制
* 调用者去处理异常,从而避免其忘记写finally,
* 以及在finally当中关闭连接.
*/
public static Connection getConnection()
throws SQLException {
return DriverManager
.getConnection(url, user, pwd);
} public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(
"关闭连接失败", e);
}
}
} }
db.properties 文件,存放连接数据库的配置:
1.文件名必须以“properties”结尾;
2.文件里边不能有中文,注释也不能是中文;
# db connection parameters
# key=value
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.201.227:1521:orcl
user=openlab
pwd=open123