摘要:preparedstatement实现增删查改的操作: Statement 和 PreparedStatement之间的关系和区别. 关系:PreparedStatement继承自Statement,都是接口 区别:PreparedStatement可以使用占位符,是预编译的,批处理比Statement效率高
详解:
1、PreparedStatement:表示预编译的 SQL 语句的对象。 接口:public interface PreparedStatement extends Statement之间的继承关系 SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。 注:用于设置 IN 参数值的设置方法(setShort、setString 等等)必须指定与输入参数的已定义 SQL 类型兼容的类型。例如,如果 IN 参数具有 SQL 类型 INTEGER,那么应该使用 setInt 方法,问号的位置也是应该注意的,因为第一个问好的位置为1,第二个问号的位置为2.以此类推。
2、我觉的最好的就是不用在进行数据的拼接。
增加数据库的信息的操作:
package com.hm.test03; import java.sql.Connection; import java.sql.PreparedStatement; import com.hm.connect.Demo01; import com.hm.model.User; public class Demo04 { private static void add(User user) throws Exception { // TODO Auto-generated method stub Demo01 db = new Demo01(); Connection con = db.getcon(); String sql = "insert into t_user value(?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, user.getId()); pstmt.setString(2, user.getName()); pstmt.setString(3, user.getPassword()); int result = pstmt.executeUpdate(); pstmt.close(); db.closeCon(con); System.out.println(result); } public static void main(String[] args) throws Exception { User user = new User("48","dwubudw","ehubfeb"); add(user); } }
修改数据库的信息:
package com.hm.test03; import java.sql.Connection; import java.sql.PreparedStatement; import com.hm.connect.Demo01; import com.hm.model.User; public class Demo1 { private static Demo01 db = new Demo01(); public static int updatebook (User user) throws Exception { Connection con = db.getcon(); String sql = "update t_user set username = ?,password = ? where id = ?"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1,user.getName() ); pstmt.setString(2,user.getPassword()); pstmt.setString(3, user.getId()); int result = pstmt.executeUpdate(); pstmt.close(); db.getcon(); return result; } public static void main(String[] args) throws Exception { User user = new User("2","nijao","ebfekbw"); int result = updatebook(user); System.out.println(result); } }
删除数据信息的操作:
package com.hm.test03; import java.sql.Connection; import java.sql.PreparedStatement; import com.hm.connect.Demo01; import com.hm.model.User; public class Demo2 { private static Demo01 db = new Demo01 (); private static int delete(User user ) throws Exception { // TODO Auto-generated method stub Connection con = db.getcon(); String sql = "delete from t_user where id = ?"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, user.getId()); int result = pstmt.executeUpdate(); pstmt.close(); db.closeCon(con); return result; } public static void main(String[] args) throws Exception { User user = new User("5","hdeu","heufuwbfu"); int result = delete(user); System.out.println(result); } }
查看数据库信息的操作:(三种方式不同的查看方式)
package com.hm.test03; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.hm.connect.Demo01; import com.hm.model.User; public class Demo03 { private static Demo01 db = new Demo01(); private static void list() throws Exception { Connection con = db.getcon(); String sql = "select *from t_user"; PreparedStatement pstmt = con.prepareStatement(sql);//預處理進行占位使用 ResultSet rs = pstmt.executeQuery(); while (rs.next()) { String id = rs.getString(1); String name = rs.getString(2); String password = rs.getString(3); System.out.println("id = " + id +",name = " + name + ",password = " + password); } } private static void list1() throws Exception { Connection con = db.getcon(); String sql = "select *from t_user"; PreparedStatement pstmt = con.prepareStatement(sql);//預處理進行占位使用 ResultSet rs = pstmt.executeQuery(); while (rs.next()) { String id = rs.getString("id"); String name = rs.getString("userName"); String password = rs.getString("password"); System.out.println("id = " + id +",name = " + name + ",password = " + password); } } private static List<User> list3() throws Exception{ List <User> userlist = new ArrayList<User>(); Connection con = db.getcon(); String sql = "select *from t_user"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { String id = rs.getString("id"); String name = rs.getString("userName"); String password = rs.getString("password"); User user = new User(id, name, password); userlist.add(user); } return userlist; } public static void main(String[] args) throws Exception { list(); System.out.println("==============================="); list1(); System.out.println("==============================="); List <User> userlist = list3(); for (User user : userlist) { System.out.println(user); } } }