一、Statement
1.通过调用 Connection 对象的 createStatement 方法创建该对象
2.该对象用于执行静态的 SQL 语句,并且返回执行结果
3.Statement 接口中定义了下列方法用于执行 SQL 语句:
ResultSet excuteQuery(String sql)
int excuteUpdate(String sql)
程序----向数据表中插入一条记录
/**
* 通过 JDBC向指定的数据表中插入一条记录
* 1. Statement: 用于执行 SQL语句的对象
* 1.1 通过 Connection的 createStatement()方法来获取
* 1.2 通过 executeUpdate(sql)可以执行 SQL语句
* 1.3 传入的 SQL可以是 INSERT、UPDATE或 DELETE,但不能是 SELECT。
*
* 2. Connection,Statement都是程序与数据库的连接资源,使用后一定要关闭
* 需要在finally中关闭
*/
@Test
public void test5() throws Exception {
Connection conn = null;
Statement statement = null;
try {
//1. 获取数据库连接
conn = getConnection2();
//2. 准备插入的SQL语句
String sql = "INSERT INTO Student " +
"VALUES('201415311', '张明', '男', 19, 'MA', '2014-09-08');";
//3. 执行插入
//3.1 获取操作SQL语句的Statement对象
// 调用Connection的createStatement()方法来获取
statement = conn.createStatement();
//3.2 通过Statement对象的executeUpdate(sql)执行SQL语句进行插入
statement.executeUpdate(sql);
} catch(Exception e) {
e.printStackTrace();
} finally {
try{
//4. 关闭Statement对象
if(statement != null) {
statement.close();
}
}catch(Exception e1) {
e1.printStackTrace();
}finally {
//5. 关闭连接
if(conn != null) {
conn.close();
}
}
}
}
二、ResultSet
1.通过调用 Statement 对象的 excuteQuery() 方法创建该对象
2.ResultSet 对象以逻辑表格的形式封装了执行数据库操作的结果集,ResultSet 接口由数据库厂商实现
3.ResultSet 对象维护了一个指向当前数据行的游标,初始的时候,游标在第一行之前,可以通过 ResultSet 对象的 next() 方法移动到下一行
4.ResultSet 接口的常用方法:
boolean next()
getString()
程序----使用ResultSet执行查询操作
/**
* ResultSet : 结果集,封装了使用 JDBC进行查询的结果。
* 1. 调用 Statement 对象的executeQuery(sql) 可以得到结果集
* 2. ResultSet 返回的实际上就是一张数据表,有一个指针指向数据表的第一行的前面,
* 可以使用 next() 方法检测下一行是否有效,若有效返回 true且指针下移
* 3. 当指针对位到一行时,可以随时调用getXxx(index)或 getXxx(column)获取每一列的值
* 例如: getString("name")
* 4. ResultSet 也需要关闭
*/
@Test
public void test7() {
Connection conn = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1. 获取连接
conn = getConnection2();
//2. 获取Statement
statement = conn.createStatement();
//3.准备SQL
String sql = "SELECT * FROM Student " +
"WHERE Sname='刘晨'";
//4. 执行查询,得到ResultSet
resultSet = statement.executeQuery(sql);
//5. 处理ResultSet
while(resultSet.next()) {
//按列读取
String no = resultSet.getString(1);
String name = resultSet.getString(2);
String sex = resultSet.getString(3);
int age = resultSet.getInt(4);
String dept = resultSet.getString(5);
Date entrance = resultSet.getDate(6);
System.out.println(no+" "+name+" "+sex+" "
+age+" "+dept+" "+entrance);
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(resultSet != null) {
try {
resultSet.close();
} catch(Exception e) {
e.printStackTrace();
}
}
if(statement != null) {
try {
statement.close();
} catch(Exception e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
附:
getConnection2()方法
//获取数据库连接
public Connection getConnection2() throws Exception {
//1.准备连接数据库的4个字符串
//1.1 创建Properties对象
Properties properties = new Properties();
//1.2 获取jdbc.properties对应的输入流
java.io.InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
//1.3 加载文件
properties.load(in);
//1.4 给字符串赋值
String driver = properties.getProperty("driver");
String jdbcUrl = properties.getProperty("jdbcUrl");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
//2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块)
Class.forName(driver);
//3.通过DriverManager的getConnection方法获取数据库连接
return DriverManager.getConnection(jdbcUrl, user, password);
}