2014-09-22 19 views
0

我有一個代碼,如下所示的數據訪問對象。在註冊表中成功插入數據時,我只想提取該特定行的註冊表的CustId。所以我使用UserName列來提取readCustomer()方法中的特定用戶CustId。該方案如下圖所示:mysql中的運行時異常

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

您可以發佈上述代碼創建的查詢嗎?註冊表的定義是什麼?您是否試圖調試您的代碼用戶名包含的值? – JavaBeigner 2014-09-22 08:34:41

+0

你能提供完整的堆棧跟蹤嗎? – CodeNewbie 2014-09-22 08:37:32

+0

@JavaBeigner請參閱編輯的問題 – user3349720 2014-09-22 08:37:35

回答

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) { 
       } 
      } 
     } 

    }