我有一个如下所示的数据访问对象代码。在注册表中成功插入数据后,我想只提取该特定行的注册表的CustId。因此,我使用UserName列在readCustomer()方法中提取特定用户CustId。该计划如下所示:
public class RegisterDAO {
public static int custId;
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public void createCustomer(Register register) {
String sql = "INSERT INTO signup "
+ "(CustId,FirstName,LastName,Address1,Address2,Country,State,City,ZipCode,UserName,EmailId,ContactNo,MobileNo,Status)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,'ONHOLD')";
Connection conn = null;
try {
conn = dataSource.createConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, register.getId());
ps.setString(2, register.getFirstName());
ps.setString(3, register.getLastName());
ps.setString(4, register.getAddress1());
ps.setString(5, register.getAddress2());
ps.setString(6, register.getCountry());
ps.setString(7, register.getState());
ps.setString(8, register.getCity());
ps.setInt(9, register.getZipCode());
ps.setString(10, register.getUserName());
ps.setString(11, register.getEmailId());
ps.setString(12, register.getContactNo());
ps.setString(13, register.getMobileNo());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
catch (NullPointerException e1){
}
finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
public void readCustomer(String username) {
String sql= "select * from signup where UserName= '" + username + "'";
Connection conn = null;
try {
conn = dataSource.createConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
custId =rs.getInt("CustId");
System.out.println(custId);
}
}catch (SQLException e) {
throw new RuntimeException(e);
}
catch (NullPointerException e1){
}
finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
}
但是这段代码抛出了一个运行时异常:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'sindhura' in 'where clause'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at com.encryption.DAO.RegisterDAO.readCustomer(RegisterDAO.java:87)
... 23 more
不是where子句采取字符串是什么是我的问题,如果它需要如何解决这个问题? 任何帮助表示赞赏。
以下句子引发了运行时异常
String sql= "select * from signup where UserName= '" + username + "'";
答案 0 :(得分:0)
请使用已准备好的声明和 pr.setString(...),如您已建议的那样。这是正确的readCustomer(...)方法:
public void readCustomer(String username) {
String sql= "select * from signup where UserName = ?";
Connection conn = null;
try {
conn = dataSource.createConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
custId =rs.getInt("CustId");
System.out.println(custId);
}
}catch (SQLException e) {
throw new RuntimeException(e);
}
catch (NullPointerException e1){
}
finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}