开发者

sql server datainsert error

开发者 https://www.devze.com 2022-12-11 02:15 出处:网络
I try this code this is problem to pass value in mssqlserver I pass value one page to another page in hear nrno is value come to other page but hear error is java.sql.SQLException: Invalid column name

I try this code this is problem to pass value in mssqlserver I pass value one page to another page in hear nrno is value come to other page but hear error is java.sql.SQLException: Invalid column name 'nrno'.

<!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 (开发者_StackOverflowname,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>


Don't you need the following ?

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

In your example code above you're not inserting the value of nrno, but rather the actual string nrno itself (since it's inside the double-quotes).

Going forwards, I'd investigate PreparedStatements such that you can do the following:

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

and avoid nasty quoting issues like the above, plus possible SQL injection problems (I realise the above may be an exercise or similar, but it's a good thing to be aware of).


Modify it,

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

精彩评论

暂无评论...
验证码 换一张
取 消