java中PreparedStatement类的setString用法
文档中是这样写的voidsetString(intparameterIndex,Stringx)路过的大侠们帮忙解释下,谢谢...
文档中是这样写的void setString(int parameterIndex,String x)
路过的大侠们帮忙解释下,谢谢 展开
路过的大侠们帮忙解释下,谢谢 展开
3个回答
展开全部
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();
}
}
2013-09-20
展开全部
conn = DriverManager.getConnection(url, userName, password);//自己链接数据库
PreparedStatement pstmtInsert = conn.prepareStatement("INSERT INTO student VALUES(?, ?, ?, ?)"); // 创建语句,里面的参数等可以用问号代替
pstmtInsert.setString(1,"a");//给第一个问号赋值"a";
pstmtInsert.setString(2,"b");//给第二个问号赋值"b";
...................
PreparedStatement pstmtInsert = conn.prepareStatement("INSERT INTO student VALUES(?, ?, ?, ?)"); // 创建语句,里面的参数等可以用问号代替
pstmtInsert.setString(1,"a");//给第一个问号赋值"a";
pstmtInsert.setString(2,"b");//给第二个问号赋值"b";
...................
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
setstring是给你的sql中的一个参数赋值,使用preparedstatement你的sql一般应该是这样的,包含一个?的占位符,用于设置参数:string
sql
=
"select
*
from
user
u
where
u.name=?";
setstring的作用就是给?赋值,第一个参数是?的位置,从1开始;第二个参数就是值了
sql
=
"select
*
from
user
u
where
u.name=?";
setstring的作用就是给?赋值,第一个参数是?的位置,从1开始;第二个参数就是值了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询