标签:c style class blog code java
1 stmt=conn.CreateStatement(); 2 resultSet rs=stmt.executeQuery(sql); 3 上面是statement的用法 4 ============================ 5 下面是PrepareStatement的用法 6 ptmt=conn.PreparedStatement(sql); 7 resultSet rs=ptmt.executeQuery(); 8 ================================ 9 Statement 是PreparedStatement的父类 10 还有就是sql放置的位置不同 。 11 在开发中一般用PrepareStatement 12
1 package jstarproject; 2 import java.sql.*; 3 public class mypreparedstatement { 4 private final string db_driver="com.microsoft.jdbc.sqlserver.sqlserverdriver"; 5 private final string url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs"; 6 public mypreparedstatement() 7 { 8 } 9 public void query() throws sqlexception{ 10 connection conn = this.getconnection(); 11 string strsql = "select emp_id from employee where emp_id = ?"; 12 preparedstatement pstmt = conn.preparestatement(strsql); 13 pstmt.setstring(1,"pma42628m"); 14 resultset rs = pstmt.executequery(); 15 16 while(rs.next()){ 17 string fname = rs.getstring("emp_id"); 18 system.out.println("the fname is " + fname); 19 } 20 rs.close(); 21 pstmt.close(); 22 conn.close(); 23 } 24 private connection getconnection() throws sqlexception{ 25 // class. 26 connection conn = null; 27 try { 28 class.forname(db_driver); 29 conn = drivermanager.getconnection(url,"sa","sa"); 30 } 31 catch (classnotfoundexception ex) {} 32 return conn; 33 } 34 //main 35 public static void main(string[] args) throws sqlexception { 36 mypreparedstatement jdbctest1 = new mypreparedstatement(); 37 jdbctest1.query(); 38 } 39 }
1 stmt.executeUpdate("insert into tb_name (col1,col2,col2,col4) values (‘"+var1+"‘,‘"+var2+"‘,"+var3+",‘"+var4+"‘)"); 2 3 4 perstmt = con.prepareStatement("insert into tb_name (col1,col2,col2,col4) values (?,?,?,?)"); 5 perstmt.setString(1,var1); 6 perstmt.setString(2,var2); 7 perstmt.setString(3,var3); 8 perstmt.setString(4,var4); 9 perstmt.executeUpdate();
prepareStatement与Statement的区别,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/comrd/p/3756344.html