preparedStatement可不可以传个数组代替setString
publicResultSetexecuteQuery(){try{stmt=DriverManager.getConnection(sConnStr,sqluser,s...
public ResultSet executeQuery(){
try {
stmt=DriverManager.getConnection(sConnStr,sqluser,sqlpassword);
PreparedStatement perstmt=conn.prepareStatement("select * from userinfo where userName=? and passWord=?");
perstmt.setString(1,"admin");
perstmt.setString(2,"admin");
rs=conn.executeQuery();
}catch (Exception ex) {
System.err.println("sqlConn.executeQuery:"+ex.getMessage());
}
return rs;
}
我想请教高手,有没有什么方法能达到传一个数组进去替换两个?号,因为我想把这个方法写死,以后直接传Sql语句和一个数组,让它自动替换里面的,我记得.net有这种方法,我把想要的效果再整理如下:
public ResultSet executeQuery(String sql,Array para[]){
try {
stmt=DriverManager.getConnection(sConnStr,sqluser,sqlpassword);
PreparedStatement perstmt=conn.prepareStatement(sql);
para[]//想在这里实体替换,该怎么做呢
rs=conn.executeQuery();
}catch (Exception ex) {
System.err.println("sqlConn.executeQuery:"+ex.getMessage());
}
return rs;
}
调用页:
Array para[]=new Array();
rs=stmt(sql,para)
效果大概就是这种,不知道有没有方法能实现,如果能,麻烦大侠们写上代码,如何定义,如何调用,小弟在此万分感激啦!
上面写错了点,rs=conn.executeQuery();应该是rs=perstmt.executeQuery();反正这个不是重点,相信大家都看的懂,哈哈 展开
try {
stmt=DriverManager.getConnection(sConnStr,sqluser,sqlpassword);
PreparedStatement perstmt=conn.prepareStatement("select * from userinfo where userName=? and passWord=?");
perstmt.setString(1,"admin");
perstmt.setString(2,"admin");
rs=conn.executeQuery();
}catch (Exception ex) {
System.err.println("sqlConn.executeQuery:"+ex.getMessage());
}
return rs;
}
我想请教高手,有没有什么方法能达到传一个数组进去替换两个?号,因为我想把这个方法写死,以后直接传Sql语句和一个数组,让它自动替换里面的,我记得.net有这种方法,我把想要的效果再整理如下:
public ResultSet executeQuery(String sql,Array para[]){
try {
stmt=DriverManager.getConnection(sConnStr,sqluser,sqlpassword);
PreparedStatement perstmt=conn.prepareStatement(sql);
para[]//想在这里实体替换,该怎么做呢
rs=conn.executeQuery();
}catch (Exception ex) {
System.err.println("sqlConn.executeQuery:"+ex.getMessage());
}
return rs;
}
调用页:
Array para[]=new Array();
rs=stmt(sql,para)
效果大概就是这种,不知道有没有方法能实现,如果能,麻烦大侠们写上代码,如何定义,如何调用,小弟在此万分感激啦!
上面写错了点,rs=conn.executeQuery();应该是rs=perstmt.executeQuery();反正这个不是重点,相信大家都看的懂,哈哈 展开
2个回答
展开全部
setString是定义了字符串中第n个”?“字符的替换。
举例:
string strsql = "select emp_id from employee where emp_id = ?";
preparedstatement pstmt = conn.preparestatement(strsql);
pstmt.setstring(1,"pma42628m");
解释:此时sql中的"?"的值就是”pma42628m“。
代码完整举例:
import java.sql.*;
public class mypreparedstatement {
private final string db_driver="com.microsoft.jdbc.sqlserver.sqlserverdriver";
private final string url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs";
public mypreparedstatement()
{
}
public void query() throws sqlexception{
connection conn = this.getconnection();
string strsql = "select emp_id from employee where emp_id = ?";
preparedstatement pstmt = conn.preparestatement(strsql);
pstmt.setstring(1,"pma42628m");
resultset rs = pstmt.executequery();
while(rs.next()){
string fname = rs.getstring("emp_id");
system.out.println("the fname is " + fname);
}
rs.close();
pstmt.close();
conn.close();
}
private connection getconnection() throws sqlexception{
// class.
connection conn = null;
try {
class.forname(db_driver);
conn = drivermanager.getconnection(url,"sa","sa");
}
catch (classnotfoundexception ex) {}
return conn;
}
//main
public static void main(string[] args) throws sqlexception {
mypreparedstatement jdbctest1 = new mypreparedstatement();
jdbctest1.query();
}
}
举例:
string strsql = "select emp_id from employee where emp_id = ?";
preparedstatement pstmt = conn.preparestatement(strsql);
pstmt.setstring(1,"pma42628m");
解释:此时sql中的"?"的值就是”pma42628m“。
代码完整举例:
import java.sql.*;
public class mypreparedstatement {
private final string db_driver="com.microsoft.jdbc.sqlserver.sqlserverdriver";
private final string url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs";
public mypreparedstatement()
{
}
public void query() throws sqlexception{
connection conn = this.getconnection();
string strsql = "select emp_id from employee where emp_id = ?";
preparedstatement pstmt = conn.preparestatement(strsql);
pstmt.setstring(1,"pma42628m");
resultset rs = pstmt.executequery();
while(rs.next()){
string fname = rs.getstring("emp_id");
system.out.println("the fname is " + fname);
}
rs.close();
pstmt.close();
conn.close();
}
private connection getconnection() throws sqlexception{
// class.
connection conn = null;
try {
class.forname(db_driver);
conn = drivermanager.getconnection(url,"sa","sa");
}
catch (classnotfoundexception ex) {}
return conn;
}
//main
public static void main(string[] args) throws sqlexception {
mypreparedstatement jdbctest1 = new mypreparedstatement();
jdbctest1.query();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询