2.如何执行sql语句,以查询语句为例
1 public class UserVoDaoImpl implements UserVoDao { 2 private Connection con; 3 4 public UserVoDaoImpl() { 5 con = new DbConnection().getDbConnection(); 6 } 7 8 // 从数据库查询用户 9 public boolean findUserVo(UserVo vaule) { 10 boolean flag = false ; 11 ResultSet set = null ; 12 13 // 1、定义预加载类PreparedStatement 14 PreparedStatement pstmt = null ; 15 16 // 2、定义sql语句 17 String sql = "seclet * from user_table where id=? pwd=? type=?" ; 18 19 // 3、预加载sql 20 try { 21 pstmt = con.prepareStatement(sql); 22 } catch (SQLException e1) { 23 e1.printStackTrace(); 24 } 25 26 // 4、用封装好信息的对象,传参给占位符 27 try { 28 // 下面这段还可以再封装一下 29 pstmt.setString(1 , vaule.getId()); 30 pstmt.setString(2 , vaule.getPwd()); 31 pstmt.setString(3 , vaule.getType()); 32 } catch (SQLException e2) { 33 e2.printStackTrace(); 34 } 35 36 // 5、执行完整的sql句 37 try { 38 // 用ResultSet对象装查询到的结果,要是没有查询到,set.next() =false 39 set= pstmt.executeQuery(); 40 if (set.next()) { 41 flag = true ; 42 } 43 } catch (SQLException e) { 44 e.printStackTrace(); 45 } 46 return flag; 47 } 48 49 }
jdbc连接数据库和执行sql语句
标签:exec exce 连接数据库 lse jdb get HERE 字符 cut
查看更多关于jdbc连接数据库和执行sql语句的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did118259