jdbc访问数据库时绑定和不绑定变量的测试例子:
import java.sql.*;
import oracle.jdbc.driver.*;
class ConOra {
public static void main(String args[] ) throws SQLException{
DriverManager.registerDriver(neworacle.jdbc.driver.OracleDriver());
Connection conn =DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.152:1521:whx","system","lukewhx");
PreparedStatement stmt ;
ResultSet rset ;
String v_sql;
/*不绑定
for (int i =1;i<=1000;i++){
v_sql="select object_name from objects where object_id="+i;
stmt =conn.prepareStatement(v_sql);
rset=stmt.executeQuery();
stmt.close();
}
*/
//绑定
for (int i =1 ;i<=1000;i++ ) {
v_sql = "select object_name from objects where object_id= :x ";
stmt=conn.prepareStatement(v_sql);
stmt.setString(1,Integer.toString(i));
rset = stmt.executeQuery();
stmt.close();
}
System.out.println("Execute OK"); } }