主题:[讨论]JDBC连接SQL出错??
为什么下面代码在执行时会出错??请大家指教!
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*"%>
<%!
String user;
String password;
Connection conn;
PreparedStatement ps;
ResultSet rs;
String dbdriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pet";
%>
<%
request.setCharacterEncoding("GB2312");
user=request.getParameter("user");
password=request.getParameter("password");
try
{
Class.forName(dbdriver).newInstance();
conn=DriverManager.getConnection(url,"sa","123456");
ps=conn.prepareStatement("insert into message (user,password) values(?,?)");
ps.setString(1,user);
ps.setString(2,password);
rs=ps.executeQuery();
while(rs.next())
{
%>
<%=rs.getString(1)%><br>
<%
}
rs.close();
ps.close();
conn.close();
}
catch(Exception e)
{
out.println(e);
}
%>
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'user' 附近有语法错误。
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*"%>
<%!
String user;
String password;
Connection conn;
PreparedStatement ps;
ResultSet rs;
String dbdriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pet";
%>
<%
request.setCharacterEncoding("GB2312");
user=request.getParameter("user");
password=request.getParameter("password");
try
{
Class.forName(dbdriver).newInstance();
conn=DriverManager.getConnection(url,"sa","123456");
ps=conn.prepareStatement("insert into message (user,password) values(?,?)");
ps.setString(1,user);
ps.setString(2,password);
rs=ps.executeQuery();
while(rs.next())
{
%>
<%=rs.getString(1)%><br>
<%
}
rs.close();
ps.close();
conn.close();
}
catch(Exception e)
{
out.println(e);
}
%>
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'user' 附近有语法错误。