PreparedStatement实现数据查询

PreparedStatement实现数据查询

所有数据的操作过程之中数据更新和数据的查询操作如果可以使用,那么基本上就可以直接上手开发了,如果使用PreparedStatement 接口来实现数据查询本质和Statement 接口中的数据查询的实现机制是非常类似的,唯一的区别就在于,执行查询的时候PreparedStatement不再需要重复配置SQL,下面通过几个实际的开发案例讲解查询具体实现。

  • 根据姓名查询
import java.util.Date;
import java.sql.*;

public class PreparedStatmentDemo {

    public static final String DRIVER = "com.mysql.cj.jdbc.Driver";    //数据库的驱动程序
    public static final String URL="jdbc:mysql://localhost:3306/yootk"; //连接地址
    public static final String USER = "root";   //用户名
    public static final String PASSWORD = "mysqladmin"; //密码

    public static void main(String[] args) throws Exception{



        Class.forName(DRIVER);  //将数据的驱动程序加载到容器内部
        Connection connection = DriverManager.getConnection(URL,USER,PASSWORD); //获取数据库的连接
        String sql ="SELECT uid,name,age,birthday,salary,note FROM user where name = ?"; //用占位符代替要查询的姓名
        PreparedStatement preparedStatement =  connection.prepareStatement(sql);  //创建一个PreparedStatement接口实例对象
        preparedStatement.setObject(1,"张三");//设置要查询的数据
        ResultSet resultSet =  preparedStatement.executeQuery();    //执行数据查询
        while (resultSet.next()){   //判断是否还有数据
            System.out.printf("【查询结果】编号:%s、姓名:%s、年龄:%s、生日:%s、工资:%f、介绍:%s\n",
                                resultSet.getObject(1),
                                resultSet.getObject(2),
                                resultSet.getObject(3),
                                resultSet.getObject(4),
                                resultSet.getObject(5),
                                resultSet.getObject(6)
            );
        }
        connection.close(); //关闭连接

    }

}

 【查询结果】编号:12、姓名:张三、年龄:16、生日:2000-01-01、工资:6900.000000、介绍:张三是一个很自律的人
【查询结果】编号:13、姓名:张三、年龄:16、生日:2021-02-07、工资:6900.000000、介绍:张三是一个很自律的人

  • 根据ID查询(因为主键是唯一的,所以对应的内容只会存在一行匹配数据,此时用while或者if的效果是一样的)
import java.sql.*;

public class PreparedStatmentDemo {

    public static final String DRIVER = "com.mysql.cj.jdbc.Driver";    //数据库的驱动程序
    public static final String URL="jdbc:mysql://localhost:3306/yootk"; //连接地址
    public static final String USER = "root";   //用户名
    public static final String PASSWORD = "mysqladmin"; //密码

    public static void main(String[] args) throws Exception{



        Class.forName(DRIVER);  //将数据的驱动程序加载到容器内部
        Connection connection = DriverManager.getConnection(URL,USER,PASSWORD); //获取数据库的连接
        String sql ="SELECT uid,name,age,birthday,salary,note FROM user where uid = ?"; //用占位符代替要查询的姓名
        PreparedStatement preparedStatement =  connection.prepareStatement(sql);  //创建一个PreparedStatement接口实例对象
        preparedStatement.setObject(1,13);//设置要查询的数据
        ResultSet resultSet =  preparedStatement.executeQuery();    //执行数据查询
        while (resultSet.next()){   //判断是否还有数据
            System.out.printf("【查询结果】编号:%s、姓名:%s、年龄:%s、生日:%s、工资:%f、介绍:%s\n",
                                resultSet.getObject(1),
                                resultSet.getObject(2),
                                resultSet.getObject(3),
                                resultSet.getObject(4),
                                resultSet.getObject(5),
                                resultSet.getObject(6)
            );
        }
        connection.close(); //关闭连接

    }

}

 【查询结果】编号:13、姓名:张三、年龄:16、生日:2021-02-07、工资:6900.000000、介绍:张三是一个很自律的人

  • 查询user表的全部信息,既然是全部信息就不需要占位符了
import java.sql.*;

public class PreparedStatmentDemo {

    public static final String DRIVER = "com.mysql.cj.jdbc.Driver";    //数据库的驱动程序
    public static final String URL="jdbc:mysql://localhost:3306/yootk"; //连接地址
    public static final String USER = "root";   //用户名
    public static final String PASSWORD = "mysqladmin"; //密码

    public static void main(String[] args) throws Exception{



        Class.forName(DRIVER);  //将数据的驱动程序加载到容器内部
        Connection connection = DriverManager.getConnection(URL,USER,PASSWORD); //获取数据库的连接
        String sql ="SELECT uid,name,age,birthday,salary,note FROM user"; //用占位符代替要查询的姓名
        PreparedStatement preparedStatement =  connection.prepareStatement(sql);  //创建一个PreparedStatement接口实例对象
        ResultSet resultSet =  preparedStatement.executeQuery();    //执行数据查询
        while (resultSet.next()){   //判断是否还有数据
            System.out.printf("【查询结果】编号:%s、姓名:%s、年龄:%s、生日:%s、工资:%f、介绍:%s\n",
                                resultSet.getObject(1),
                                resultSet.getObject(2),
                                resultSet.getObject(3),
                                resultSet.getObject(4),
                                resultSet.getObject(5),
                                resultSet.getObject(6)
            );
        }
        connection.close(); //关闭连接

    }

}

【查询结果】编号:1、姓名:李兴华、年龄:18、生日:2008-08-13、工资:8000.000000、介绍: www. yootk. com
【查询结果】编号:2、姓名: 沐言优折、年龄:18、生日:2009-09-15、工资:9000.000000、介绍: ww.yootk.com
【查询结果】编号:3、姓名:VIP学院、年龄:18、生日:2009-09-15、工资:78000.000000、介绍:yootk.ke.qq.com
【查询结果】编号:4、姓名:李四、年龄:17、生日:1888-08-01、工资:100.000000、介绍:www.baidu.com
【查询结果】编号:5、姓名:李四、年龄:17、生日:1888-08-01、工资:100.000000、介绍:www.baidu.com
【查询结果】编号:6、姓名:李四、年龄:17、生日:1888-08-01、工资:100.000000、介绍:www.baidu.com
【查询结果】编号:7、姓名:王五、年龄:78、生日:1947-08-01、工资:800.000000、介绍:www.baidu.com
【查询结果】编号:8、姓名:王五、年龄:78、生日:1947-08-01、工资:800.000000、介绍:www.baidu.com
【查询结果】编号:9、姓名:王五、年龄:78、生日:1947-08-01、工资:800.000000、介绍:www.baidu.com
【查询结果】编号:10、姓名:王五、年龄:78、生日:1947-08-01、工资:800.000000、介绍:www.baidu.com
【查询结果】编号:11、姓名:王五、年龄:78、生日:1947-08-01、工资:800.000000、介绍:www.baidu.com
【查询结果】编号:12、姓名:张三、年龄:16、生日:2000-01-01、工资:6900.000000、介绍:张三是一个很自律的人
【查询结果】编号:13、姓名:张三、年龄:16、生日:2021-02-07、工资:6900.000000、介绍:张三是一个很自律的人

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用PreparedStatement实现查询整个表的示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class QueryTableExample { public static void main(String[] args) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // 1. 加载数据库驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 2. 获取数据库连接 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "password"; conn = DriverManager.getConnection(url, user, password); // 3. 创建PreparedStatement对象 String sql = "SELECT * FROM mytable"; stmt = conn.prepareStatement(sql); // 4. 执行查询并获取结果集 rs = stmt.executeQuery(); // 5. 处理结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 6. 关闭资源 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的示例中,我们通过PreparedStatement对象执行了一个查询语句,查询了整个表。在处理结果集时,我们使用了ResultSet对象的next()方法来遍历结果集中的每一行数据,并使用getInt()、getString()等方法获取每一列的值。最后,我们在finally块中关闭了所有资源,以确保程序正确释放资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值