刚开始学Jv跟着视频做了个登录界面验证结果com.microsoft.sqlserver.jdbc.SQLServerException: 索引 2 超出范围。
com.microsoft.sqlserver.jdbc.SQLServerException: 索引 2 超出范围。
用户名:user密码:123456
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:714)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:723)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setString(SQLServerPreparedStatement.java:1048)
at db.SqlHelper.query(SqlHelper.java:38)
at Dao.UserMode.checkUser(UserMode.java:26)
at Dao.Index.actionPerformed(Index.java:87)
代码如下-1:
package Dao;
import java.awt.EventQueue;
public class Index extends JFrame implements ActionListener {
private JPanel contentPane;
private JTextField textName;
private JLabel label;
private JTextField textPsw;
private JButton btnNewButton;
* Launch the application.
public static void main(String[] args) {
/**
* Create the frame.
*/
public Index() {
@Override
//响应用户请求。
public void actionPerformed(ActionEvent e) {
Object obe = e.getSource();
//判断是否点击登录按钮
if (obe.equals( btnNewButton)) {
//取出用户名 密码
String u = this.textName.getText();
String p = this.textPsw.getText();
UserMode um =new UserMode();
String res =um.checkUser(u, p);
System.out.println("用户名:"+u+"密码:"+p);
}
}
}
代码如下-2:
package Dao;
import java.sql.ResultSet;
import db.SqlHelper;
/**
* 用户对数据库的操作
*
* */
public class UserMode {
/**
*
* @param LoginId 用户编号
* @param LoginPWD 用户密码
* @return
*/
public String checkUser(String LoginId,String LoginPWD) {
SqlHelper sp = null;
try {
//组织sql,和参数列表
String sql = "select ? from Users where LoginId add LoginPWD";
String paras[] = {LoginId,LoginPWD};
sp = new SqlHelper();
ResultSet ts = sp.query(sql, paras);
} catch (Exception e) {
// TODO: handle exception
}finally{
sp.close();
}
return null;
}
}
代码如下-3:
/**
* 对数据库操作的类
* 对数据库的操作
* 调用存储过程 主要写项目业务操作
*/
package db;
import java.sql.*;
public class SqlHelper {
//定义需要的对象
PreparedStatement ps = null;
ResultSet rs = null;
Connection ct = null;
//应为多次引用故写出来
String dirverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://127.0.0.1:1433;dataabaseName = MyBookShop";
String user = "jiashao";
String passwd = "admin";
//构造函数,初始化ct
public SqlHelper(){
try {
//加载驱动
Class.forName(dirverName);
//2.创建连接
ct = DriverManager.getConnection(url,user,passwd);
} catch (Exception e) {
e.printStackTrace();//防止程序出错,来报错提示
// TODO: handle exception
}
}
public ResultSet query(String sql,String []paras ){//数据结果都可以通过这方法得到结果
try {
ps = ct.prepareStatement(sql);
//对sql的参数赋值
for (int i = 0; i < paras.length; i++) {
ps.setString(i+1, paras[i]);
}
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return rs;
}
//关闭资源的方法
public void close(){
try {
if (rs!=null) rs.close();
if (ps!=null) ps.close();
if (ct!=null) ct.close();{
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}