package com.jdbc;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;import com.tools.MD5;
import com.vo.PageBean;
import com.vo.UserVo;import com.tools.GetDataInfo;public class DBbean {
private DataSource ds; private Connection con; public DBbean() {
try {
GetDataInfo datainfo = GetDataInfo.getInstance();
String datasourcename = datainfo.getInfo();
Context initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup(datasourcename);
if (ds != null) {
con = ds.getConnection();
System.out.println("ok");
}
} catch (Exception e) {
e.printStackTrace();
}
}




public boolean findUserInfo(UserVo uservo) {
boolean rec = true;
String sql = "select id from user where username='"+uservo.getUsername()+"'and password='"+uservo.getPassword()+"'";
PreparedStatement pstmt = null;
ResultSet rs = null; try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, uservo.getUsername());
rs = pstmt.executeQuery();
if (rs.next()) {
rec = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
rs.close();
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} return rec;
}


public void dbclose() {
try {
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的源程序,我在查找这里出了问题,刚学不是很明白,求大家帮忙解决,多谢了下面是异常
 [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.validateParameterIndex(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.setObjectInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.setString(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.setString(DelegatingPreparedStatement.java:132)
at com.jdbc.DBbean.findUserInfo(DBbean.java:53)
at com.servlet.LoginServlet.doPost(LoginServlet.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

解决方案 »

  1.   

    public boolean findUserInfo(UserVo uservo) { 
    boolean rec = true; 
    String sql = "select id from [user] where username=? and password=?"; 
    PreparedStatement pstmt = null; 
    ResultSet rs = null; try { 
    pstmt = con.prepareStatement(sql); 
    pstmt.setString(1, uservo.getUsername()); 
    pstmt.setString(2, uservo.getPassword()); 
    rs = pstmt.executeQuery(); 
    if (rs.next()) { 
    rec = true; 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } finally { 
    try { 
    if (pstmt != null) { 
    rs.close(); 
    pstmt.close(); 

    } catch (Exception e) { 
    e.printStackTrace(); 

    } return rec; 

      

  2.   


    String sql = "select id from user where username='"+uservo.getUsername()+"' and password='"+uservo.getPassword()+"'"; 自己看有什么区别, 你and前少个空格
      

  3.   

    uservo.getUsername()+"'这里也加个空格哦and password='
      

  4.   

    String sql = "select id from [user] where username=? and password=?";  
    PreparedStatement pstmt = null;  
    ResultSet rs = null;  try {  
    pstmt = con.prepareStatement(sql);  
    pstmt.setString(1, uservo.getUsername());  
    pstmt.setString(2, uservo.getPassword());  
    rs = pstmt.executeQuery();  
    这种方式要号一些。
      

  5.   

    lz的下面这句:
    String sql = "select id from user where username='"+uservo.getUsername()+"' and password='"+uservo.getPassword()+"'"; 另外我感觉这个错误不是sql语句的错误,看lz的代码,数据库的连接字符串应该是从其它地方得来的,检查一下字符串写的是否正确
      

  6.   

    在后台把sql语句输出来,有问题就拿到查询分析器上运行一下,就可以找到问题的原因了
      

  7.   

    package com.jdbc;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;import com.tools.MD5;
    import com.vo.PageBean;
    import com.vo.UserVo;import com.tools.GetDataInfo;public class DBbean {
    private DataSource ds; private Connection con; public DBbean() {
    try {
    GetDataInfo datainfo = GetDataInfo.getInstance();
    String datasourcename = datainfo.getInfo();
    Context initCtx = new InitialContext();
    ds = (DataSource) initCtx.lookup(datasourcename);
    if (ds != null) {
    con = ds.getConnection();
    System.out.println("ok");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public boolean setUserInfo(UserVo uservo) {
    boolean rec = true;
    String username = uservo.getUsername();
    String password = uservo.getPassword();
    MD5 md = new MD5();
    password = md.getMD5ofStr(password);

    String cx = uservo.getCx();
    String sql = "insert into [user] (username,password,cx,zcdate) values(?,?,?,getdate())";
    PreparedStatement pstmt = null;
    try {
    pstmt = con.prepareStatement(sql);
    pstmt.setString(1, username);
    pstmt.setString(2, password);
    pstmt.setString(3, cx);
    pstmt.executeUpdate();
    } catch (Exception e) {
    e.printStackTrace();
    rec = false;
    } finally {
    try {
    if (pstmt != null)
    pstmt.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return rec;
    }



    public void dbclose() {
    try {
    if (con != null)
    con.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    留下该过的