2009-11-09 89 views
0

我嘗試這段代碼這是在mssqlserver中傳遞值的問題我將值傳遞給另一頁中的頁面聽到nrno是值來到其他頁面但是聽到錯誤是java.sql.SQLException:無效的列名' nrno」。sql server datainsert錯誤

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>Untitled</title> 
</head> 
<body> 
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 

<% 
String AppURL = request.getContextPath() ; 
String thisFile = AppURL+request.getServletPath() ; 
int nrno = 0; 
try 
{ 
nrno = Integer.parseInt(request.getParameter("rno")); 
} 
catch(NumberFormatException ex) 
{ 
    nrno = 0; 
} 
%> 
<td>this is in roolno :- <%=nrno%> </td><br> 
<% 
Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/sample", "sa", "sa1234"); 
java.sql.Statement stmt = conn.createStatement(); 
      try 
      { 
     int val = stmt.executeUpdate("INSERT student (name,rno) VALUES('nikki',+ nrno)"); 
      out.println("1 row affected"); 
     } 
     catch (SQLException s) 
      { 
     System.out.println("SQL statement is not executed!"); 
     } 
stmt.close(); 
conn.close(); 
%> 
</body> 
</html> 
+0

PSL使用代碼格式化 – KB22 2009-11-09 11:33:30

回答

1

你不需要以下嗎?

int val = stmt.executeUpdate("INSERT student (name,rno) VALUES('nikki'," + nrno + ")"); 

在您的示例代碼上面你不插入價值nrno,而是實際的字符串nrno本身(因爲它是雙引號)。

的快進,我調查PreparedStatements,這樣你可以做到以下幾點:

PreparedStatement pstmt = con.prepareStatement("INSERT student (name,rno) VALUES(?,?)"); 
    pstmt.setString(1, 'nikki'); 
    pstmt.setInt(2, nrno); 

,並避免類似上述情況,加上可能的SQL注入問題討厭引述的問題(我知道上面可能是一個鍛鍊或類似的,但它是一件好事,要知道)。

0

修改它,

int val = stmt.executeUpdate("INSERT student (name,rno) VALUES('nikki'," + nrno 
+ ")");