您的位置:首页 > 其它

JDBC学习笔记(2)——Statement和ResultSet

2016-05-02 21:32 453 查看
Statement执行更新操作

Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。Statement对象,用于执行不带参数的简单SQL语句。

通过JDBC向指定的数据表中插入一条记录,需要注意下面的几点:

* 1.Statement:用于执行SQL语句的对象
* 1).通过COnnection的createStatement()方法来获取
* 2).通过excuteUpdate(sql)可以执行SQL语句
* 3).传入的SQL可以是insert,update或者delete,但是不能是select
* 2.Connection、Statement都是应用程序和数据库服务器的连接   资源,使用后一定要关闭
* 需要在finally中关闭Connection和Statement对象
* 异常可以不处理,但是连接一定要关闭
* 3.关闭的顺序:先关闭后获取的,即先关闭Statement,后关闭Connection


具体的代码实现:

public void testStatement() throws Exception{
//1.获取数据库连接
//        Connection conn=getConnection();
Connection conn=null;
//4.执行插入
//1).获取操作SQL语句的Statement对象:调用Connection的createStatement()方法来获取
//注意Statement这里是java.sql包中的,而不是java.mysql.jdbc中的
//        Statement statement=conn.createStatement();
Statement statement=null;
try {
//3.准备插入的SQL语句
conn=getConnection();
String sql=null;
//sql的插入操作
//            sql="insert into customers(NAME,email,birth) values('xyz','xyz@atguigu.com','1988-7-1')";
//删除操作
//            sql="delete from customers where id =1";
//修改操作
sql="update customers set name='Tom' where id =2";
statement = conn.createStatement();
//2).调用Statement对象的excuteUpdate(sql),执行SQL语句进行插入
statement.execute(sql);
//5.关闭Statement对象
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {

// 2.关闭连接
if (conn != null) {

conn.close();
}
}
}
}


【提示】:代码中的getConnction方法是在笔记一中定义的,可以看到我们可以对数据库中的记录进行插入(insert),更新(update),删除(delete)操作,使用Connection对象的createStatement( )方法创建一个statement对象,并且调用Statement对象的excuteUpdate(sql),执行SQL语句进行插入;
我们的getConnection方法和关闭statement以及conn的操作稍显复杂,我们可以定义一个工具类,里面包含一些通用的方法,实现我们的插入、删除、更新数据的操作

具体代码:

public class JDBCTools {
// 关闭conn和statement的操作
public static void release(Statement statement, Connection conn) {
if (statement != null) {
try {
statement.close();

} catch (Exception e2) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 1。获取连接的方法 通过读取配置文件从数据库服务器获取一个连接
*
* @author Administrator
*
*/
public static Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 读取类路径下的jdbc.properties文件
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
// 通过反射创建Driver对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
}


我们更新数据的操作可以写成这样:这里update就是这个通用的方法;

public void update(String sql){
Connection conn=null;
Statement statement=null;
try {
//用到了我们写的一个工具类JDBCTools
conn=JDBCTools.getConnection();
statement=conn.createStatement();
statement.execute(sql);
} catch (Exception e) {
// TODO: handle exception
}finally{
JDBCTools.release(statement, conn);
}
}


传入不同的sql,执行相应的操作;

通过ResultSet执行查询操作

ResultSet:

/**
* ResultSet:结果集,封装了使用JDBC进行查询的结果
* 1.调用Statement对象的excuteQuery(sql)方法可以得到结果集
* 2.ResultSet返回的实际上就是一张数据表,有一个指针
*   指向数据表的第一样的前面,可以调用next()方法检测下一行是否有效,若有效则返回true
*   ,并且指针下移,相当于迭代器对象的hasNext()和next()的结合体
* 3.当指针对位到确定的一行时,可以通过调用getXxx(index)或者getXxx(columnName)
* 获取每一列的值,例如:getInt(1),getString("name")
* 4.ResultSet当然也需要进行关闭
*/


ResultSet的返回结果:

package com.atguigu.jdbc;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.mysql.jdbc.Driver;

/**
* 操作JDBC的工具类,其中封装了一些工具方法
* Version 1
* @author Administrator
*
*/
public class JDBCTools {
// 关闭conn和statement的操作
public static void release(ResultSet rs,Statement statement, Connection conn) {
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
// TODO: handle exception
}
}
if (statement != null) {
try {
statement.close();

} catch (Exception e2) {
// TODO: handle exception
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* 1。获取连接的方法 通过读取配置文件从数据库服务器获取一个连接
*
* @author Administrator
*
*/
public static Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 读取类路径下的jdbc.properties文件
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
// 通过反射创建Driver对象
Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties();
info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}
}


View Code

本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/ysw-go/
1、本博客的原创原创文章,都是本人平时学习所做的笔记,如有错误,欢迎指正。
2、如有侵犯您的知识产权和版权问题,请通知本人,本人会即时做出处理文章。
3、本博客的目的是知识交流所用,转载自其它博客或网站,作为自己的参考资料的,感谢这些文章的原创人员
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: