Java使用PreparedStatement接口及ResultSet结果集的方法示例
2018-07-07 11:28
1131 查看
本文实例讲述了Java使用PreparedStatement接口及ResultSet结果集的方法。分享给大家供大家参考,具体如下:
说明:
1.PreparedStatement接口继承Statement,它的实例包含已编译的SQL语句,执行速度要快于Statement。
2.PreparedStatement继承了Statement的所有功能,三种方法
executeUpdate、
executeQuery、
execute不再需要参数。
3.在JDBC应用中,一般都用PreparedStatement,而不是Statement。
便于操作,先做一些封装:
对连接数据库,关闭连接封装,在之前博客中已经提到DbUtil.java;
对数据库表进行封装,这里是对我的数据库中comp表进行操作,因此封装如下:
package com.mysqltest.jdbc.modelComp; public class CompMember { private int id; private String name; private int age; private double salary; /** * 构造函数1 * @param name * @param age * @param salary */ public CompMember(String name, int age, double salary) { super(); this.name = name; this.age = age; this.salary = salary; } /** * 重载构造函数 * @param id * @param name * @param age * @param salary */ public CompMember(int id, String name, int age, double salary) { super(); this.id = id; this.name = name; this.age = age; this.salary = salary; } /** * get,set方法 */ public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override /** * 改写toString,使得显示更好 */ public String toString() { return "["+this.id+"]"+this.name+","+this.age+","+this.salary; } }
然后利用PreparedStatement接口实现增的操作:
package com.mysqltest.jdbc.xiao1; import java.sql.Connection; import java.sql.PreparedStatement; import com.mysqltest.jdbc.modelComp.CompMember; import com.mysqltest.jdbc.util.DbUtil; public class PstatementTest { private static DbUtil dbUtil = new DbUtil(); /** * 用PreparedStatement添加成员 * @param mem * @return * @throws Exception */ private static int addMember(CompMember mem) throws Exception{ Connection con = dbUtil.getCon(); String sql = "insert into comp values(null,?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, mem.getName()); pstmt.setInt(2, mem.getAge()); pstmt.setDouble(3, mem.getSalary()); int result = pstmt.executeUpdate();//中间不用传入sql dbUtil.close(pstmt, con); //preparedStatement是子类,用父类关闭也行 return result; } public static void main(String[] args) throws Exception { CompMember mem = new CompMember("刘翔", 24, 8000.00); int result = addMember(mem); if (result==1) { System.out.println("添加成功"); } else { System.out.println("添加失败"); } } }
再利用PreparedStatement接口实现查询,并运用ResultSet结果集:
package com.mysqltest.jdbc.xiao2; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.mysqltest.jdbc.modelComp.CompMember; import com.mysqltest.jdbc.util.DbUtil; public class ResultsetTest { private static DbUtil dbUtil = new DbUtil(); /** * 遍历查询结果 * @throws Exception */ @SuppressWarnings("unused") private static void listMem1() throws Exception { Connection con = dbUtil.getCon();// 获取连接 String sql = "select * from comp"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();// 返回结果集 // next()将光标向后一行 while (rs.next()) { int id = rs.getInt(1);// 获取第一列的值id String name = rs.getString(2);// int age = rs.getInt(3); double salary = rs.getDouble(4); System.out.println("编号:" + id + "姓名:" + name + "年龄:" + age + "工资:" + salary); System.out.println("+====================================+"); } } /** * 遍历查询结果方法2 * @throws Exception */ @SuppressWarnings("unused") private static void listMem2() throws Exception { Connection con = dbUtil.getCon();// 获取连接 String sql = "select * from comp"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();// 返回结果集 // next()将光标向后一行 while (rs.next()) { int id = rs.getInt("id");// 获取第一列的值id String name = rs.getString("name");// int age = rs.getInt("age"); double salary = rs.getDouble("salary"); System.out.println("编号:" + id + "姓名:" + name + "年龄:" + age + "工资:" + salary); System.out.println("+====================================+"); } } private static List<CompMember> listMem3() throws Exception{ List<CompMember> memList = new ArrayList<CompMember>(); Connection con = dbUtil.getCon();// 获取连接 String sql = "select * from comp"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();// 返回结果集 // next()将光标向后一行 while (rs.next()) { int id = rs.getInt("id");// 获取第一列的值id String name = rs.getString("name");// int age = rs.getInt("age"); double salary = rs.getDouble("salary"); CompMember mem = new CompMember(id, name, age, salary); memList.add(mem);//添加到List中 } return memList; } public static void main(String[] args) throws Exception { // listMem1(); // listMem2(); List<CompMember> memList = listMem3(); for (CompMember mem : memList) { //遍历集合的每个元素 System.out.println(mem); } } }
更多关于java相关内容感兴趣的读者可查看本站专题:《Java+MySQL数据库程序设计总结》、《Java数据结构与算法教程》、《Java文件与目录操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- java自定义任务类定时执行任务示例 callable和future接口使用方法
- java中接口(interface)及使用方法示例
- java.util.List接口的方法subList()的使用注意事项
- java.util.List接口的方法subList()的使用注意事项
- java.sql.resultset方法与使用技巧
- Java输入流Scanner/BufferedReader使用方法示例
- Java 中 ListIterator 接口的使用示例
- Java ThreadLocal示例及使用方法总结
- JAVA comparable接口的使用方法
- JAVA中常用接口的介绍及使用示例:java.lang.Comparable
- java通过url获取页面数据 java解析xml 基金净值接口使用方法
- java 局部内部类的一般用途是实现某个接口,并作为这个接口传出方法被使用
- Java中使用接口实现多继承和多态的方法
- java创建excel示例(jxl使用方法)
- java代码关于匿名内部类和接口的方法使用
- java.sql.resultset方法与使用技巧
- 模板方法模式(TemplateMethod Pattern)(二):java中模板方法和钩子的使用示例
- JAVA中常用接口的介绍及使用示例:java.lang.Comparable
- Java中软引用、弱引用和虚引用的使用方法示例
- java system类使用方法示例 获取系统信息