环境:TOMCAT6.0+JDK
注册代码:
<%@ page contentType= "text/html; charset=gb2312 " %>
<%@ page language= "java " import= "java.sql.* "%>
<html>
<head>
<title> 系统用户注册 </title>
</head>
<%
final String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver " ;
final String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=myjob ";
Connection conn = null ;
ResultSet rs = null ;
String name=null;
String password=null;
String user= "sa ";
String pass= " ";
String reg_name=request.getParameter( "name ");
String reg_pass=request.getParameter( "password ");
Statement st=null;
PreparedStatement stat=null;
boolean flag=true;
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,user,pass) ;
st=conn.createStatement();
rs = st.executeQuery( "select * from user1 ") ;
while(rs.next())
{
if(reg_name.equals(rs.getString(1))){
out.println( " <br> <br> <center> <B> 请重新注册 </B> </center> ");
out.println( " <script> alert( '对不起,用户名 "+reg_name+ "已存在 ');location.replace( 'reg.jsp '); </script> ");
flag=false;
break;
}
}
if (flag){
stat=conn.prepareStatement( "insert user1(user1.name,user1.pass) values( ' "+reg_name+ " ', ' "+reg_pass+ " ') ");
stat.setString(1,reg_name);
stat.setString(2,reg_pass);
stat.executeUpdate();
out.println( " <br> <br> <center> <B> 注册成功 </B> </center> ");
out.println( " <br> <br> <center> 您的用户名是: "+reg_name+ " </center> ");
out.println( " <br> <center> 您的密码是: "+reg_pass+ " </center> ");
stat.close();
String name_reged=null;
session.putValue( "name_reged ",reg_name);
out.println( " <script> alert( '谢谢注册,请登陆 ');location.replace( 'index.jsp ') </script> ");
}
rs.close() ;
conn.close() ;
}
catch(Exception e)
{
out.println(e);
}
%>
</html>
报错:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
请问什么原因?
------解决方法--------------------------------------------------------
因为你有:
stat.setString(1,reg_name);
stat.setString(2,reg_pass);
所以要把
stat=conn.prepareStatement( "insert user1(user1.name,user1.pass) values( ' "+reg_name+ " ', ' "+reg_pass+ " ') ");
改成
stat = conn.prepareStatement( "insert user1(user1.name,user1.pass) values(?,?) ");
否则它找不到匹配的参数。