JDBC知识学习——Statement执行更新和ResultSet查询操作

Statement用于执行sql语句的对象:

1.通过Connection的createStatement()方法来获取
2.通过executeUpdate(sql)执行sql语句,可以执行insert,update,delete
代码示例:

public class StatementTest {
    /*
    * ResultSet结果集封装了使用JDBC查询得到的结果
    * 1.调用Statement对象的executeQuery(sql)得到结果集
    * 2.ResultSet返回的是一张数据表
    * */
    @Test
    public void testResultSet() throws Exception{
        //获取指定id的数据并打印
        //1.获取连接
        Connection connection=getConnection();
        //2.获取statement
        Statement statement=connection.createStatement();
        //3.准备sql
        String sql="select id,name,pwd,sex,home,info from user where id=8";
        String sql1="select id,name,pwd,sex,home,info from user " ;//查询多条数据
        //4.执行查询
        ResultSet re = statement.executeQuery(sql1);
        //5.处理得到的ResultSet
    while(re.next()){//if(re.next()){
        int id=re.getInt(1);
        String name=re.getString("name");
        String sex=re.getString(4);
        String info=re.getString(5);
        System.out.println(id+"\t"+name+"\t"+sex+"\t"+info);
    }
        //6.关闭数据库资源
        re.close();
        JDBCTools.releaseSource(statement,connection);
    }

    @Test
    public void test1() {
        Connection con = null;
        Statement statement = null;
        try {
            //获取数据库连接
            con = getConnection();
            //准备插入的sql语句
            String sql = "insert into user(name,pwd,sex,home,info)" +
                    "values('Roy','111','Man','Lz','sky');";
            String sql1 = "delete from user where id=9;";
            String sql2 = "update user set name='Jerry' where id=6;";

            //执行插入操作
            //1.获取执行sql语句的Statement对象
            statement = con.createStatement();
            //2调用方法进行插入
            statement.executeUpdate(sql2);
            System.out.println("sql执行成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTools.releaseSource(statement,con);
            /*if (statement != null)
            //关闭Statement对象
            {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null)
            //关闭连接
            {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }*/

        }
    }
    /**
    *@ClassName StatementTest
    *@Description 获取数据库资源连接
    *@Param []
    *@Return java.sql.Connection
    *@Date 2020/2/13 15:57
    *@Author Roy
    */
    public Connection getConnection() throws Exception {
        //连接数据库的字符串
        String driverClass = null;//驱动的全类名
        String jdbcUrl = null;
        String user = null;
        String password = null;
        //读取类路径下的jdbc.properties文件
        InputStream in =
                this.getClass().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");
        //加载数据库驱动程序
        Class.forName(driverClass);
        //获取数据库连接
        Connection connection1 = DriverManager.getConnection(jdbcUrl, user, password);
        return connection1;
    }
}

操作JDBC的工具类封装了工具方法

代码示例:

public class JDBCTools {
    /**
    *@ClassName JDBCTools
    *@Description 读取连接的方法通过读取配置文件获取数据库服务器连接
    *@Param []
    *@Return java.sql.Connection
    *@Date 2020/2/13 15:51
    *@Author Roy
    */
    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 driver1=(Driver) Class.forName(driverClass).newInstance();
        Properties inf=new Properties();
        inf.put("user",user);
        inf.put("password",password);
        //获取数据库连接
        Connection connection1=driver1.connect(jdbcUrl,inf);
        return connection1;
    }
    /**
    *@ClassName JDBCTools
    *@Description 释放资源,关闭statement和Connection
    *@Param [statement, connection]
    *@Return void
    *@Date 2020/2/13 15:54
    *@Author Roy
    */

    public static void releaseSource(Statement statement,Connection connection){
        if (statement != null)
        //关闭Statement对象
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null)
        //关闭连接
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值