一、JDBC简介
ResultSet rs = st.executeQuery(sql);
1 //查询操作 2 Statement st = conn.createStatement(); 3 String sql = “select * from user where id=1; 4 ResultSet rs = st.executeQuery(sql); 5 while(rs.next()){ 6 //根据获取列的数据类型,分别调用rs的相应方法 7 //映射到java对象中 8 } 9 10 //插入操作 11 String sql = "insert into user(….) values(…..) "; 12 int num = st.executeUpdate(sql); 13 if(num>0){ 14 System.out.println("插入成功!!!"); 15 } 16 //修改操作 17 String sql = “update user set name=‘’ where name=‘’"; 18 int num = st.executeUpdate(sql); 19 if(num>0){ 20 System.out.println("修改成功!!!"); 21 } 22 //删除操作 23 String sql = “delete from user where id=1; 24 int num = st.executeUpdate(sql); 25 if(num>0){ 26 System.out.println("删除成功!!!"); 27 }
7、PreparedSatement类详解:
1 String insertString = "insert into users values(?,?,?,?);"; 2 //id, name, password, email 3 4 PreparedStatement preparedStatement= connection.prepareStatement(insertString); 5 preparedStatement.setInt(1, 1); 6 preparedStatement.setString(2, "zhangsan"); 7 preparedStatement.setString(3, "123456"); 8 preparedStatement.setString(4, "[email protected]"); 9 preparedStatement.execute();
8、使用JDBC处理大文本和大二进制数据:
1 //把一个文件里的内容保存到 text 列中 2 File file = new File("1.txt"); 3 FileReader fileReader = new FileReader(file); 4 String insertsString ="insert into lobtest values(?,?)"; 5 PreparedStatement ps = connection.prepareStatement(insertsString); 6 7 /* void setCharacterStream(int parameterIndex, Reader reader) 8 将指定参数设置为给定 Reader 对象。 9 //mysql 仅支持这个方法 10 void setCharacterStream(int parameterIndex, Reader reader, int length) 11 将给定参数设置为给定 Reader 对象,该对象具有给定字符数长度。 12 void setCharacterStream(int parameterIndex, Reader reader, long length) 13 将指定参数设置为给定 Reader 对象,该对象具有给定字符数长度。 14 15 */ 16 ps.setInt(1, 1); 17 ps.setCharacterStream(2,fileReader,(int)file.length()); 18 ps.execute(); 19 //把一个text列中的内容存储到一个文件中 20 File file = new File("2.txt"); 21 FileWriter fileWriter = new FileWriter(file); 22 String queryString ="select content from lobtest where id = ?"; 23 PreparedStatement ps = connection.prepareStatement(queryString); 24 ps.setInt(1, 1); 25 ps.execute(); 26 rs=ps.getResultSet(); 27 rs.next(); 28 Reader reader = rs.getCharacterStream(1); 29 30 char[] ch = new char[1024]; 31 int len = 0; 32 while ((len=reader.read(ch, 0, 1024))!=-1) { 33 fileWriter.write(ch, 0, len); 34 } 35 reader.close(); 36 fileWriter.close();
//先将一张图片放入到数据库中 String insertString = "insert into lobtest2 values(?,?);"; PreparedStatement prepareStatement = connection.prepareStatement(insertString) File file = new File("zixia.jpg"); FileInputStream fis= new FileInputStream(file); prepareStatement.setInt(1, 2); prepareStatement.setBinaryStream(2, fis,(int)file.length()); prepareStatement.execute(); //在从数据库中读出一张照片到文件中 String qureyString ="select context from lobtest2 where id = ?"; PreparedStatement prepareStatement = connection.prepareStatement(qureyString); prepareStatement.setInt(1, 2); prepareStatement.execute(); ResultSet resultSet = prepareStatement.getResultSet(); resultSet.next(); InputStream stream = resultSet.getBinaryStream(1); FileOutputStream fos = new FileOutputStream("mm.jpg"); byte[] b=new byte[1024]; int len=0; while ((len=stream.read(b, 0, 1024))!=-1) { fos.write(b, 0, len); } fos.close(); stream.close();
9、使用JDBC进行批处理:
1 conn = JdbcUtil.getConnection(); 2 String sql = "insert into user(name,password,email,birthday) values(?,?,?,?)"; 3 st = conn.prepareStatement(sql); 4 for(int i=0;i<50000;i++){ 5 st.setString(1, "aaa" + i); 6 st.setString(2, "123" + i); 7 st.setString(3, "aaa" + i + "@sina.com"); 8 st.setDate(4,new Date(1980, 10, 10)); 9 st.addBatch(); 10 if(i%1000==0){ 11 st.executeBatch(); 12 st.clearBatch(); 13 } 14 } 15 st.executeBatch();
优点:发送的是预编译后的SQL语句,执行效率高。
1 CallableStatement cStmt = conn.prepareCall("{call demoSp(?, ?)}"); 2 cStmt.setString(1, "abcdefg"); 3 cStmt.registerOutParameter(2, Types.VARCHAR); 4 cStmt.execute(); 5 System.out.println(cStmt.getString(2));