这个是注册界面
package page;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import Dao.UserDao;
import Dao.UserDao2;
import Model.User;
import java.awt.Font;
import java.awt.Image;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
public class Register {
private JFrame frame;
private JTextField username;
private JTextField password;
private JTextField password_c;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register window = new Register();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Register() {
initialize();
//让窗口显示
frame.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("\u6CE8\u518C\u9875\u9762");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(Register.class.getResource("/image/background1.png")));
frame.setBounds(100, 100, 599, 448);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D");
lblNewLabel_1.setFont(new Font("隶书", Font.BOLD, 24));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setBounds(76, 44, 139, 51);
frame.getContentPane().add(lblNewLabel_1);
username = new JTextField();
username.setBounds(252, 48, 252, 51);
frame.getContentPane().add(username);
username.setColumns(10);
JLabel label = new JLabel("\u5BC6 \u7801");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("隶书", Font.BOLD, 24));
label.setBounds(76, 105, 139, 51);
frame.getContentPane().add(label);
password = new JTextField();
password.setColumns(10);
password.setBounds(252, 109, 252, 51);
frame.getContentPane().add(password);
JLabel label_1 = new JLabel("\u786E\u8BA4\u5BC6\u7801");
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setFont(new Font("隶书", Font.BOLD, 24));
label_1.setBounds(76, 175, 139, 51);
frame.getContentPane().add(label_1);
password_c = new JTextField();
password_c.setColumns(10);
password_c.setBounds(252, 170, 252, 51);
frame.getContentPane().add(password_c);
JButton btnNewButton = new JButton("\u6CE8 \u518C");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//获取密码框和确认密码框,看是否相同
String password_v = password.getText();
String password_c_v = password_c.getText();
if(password_v.equals(password_c_v));{
//弹出框,提示用户输入的两次密码不相等
JOptionPane.showMessageDialog(null,"两次输入密码不相同");
return;
}
//【下面这一行是报错的地方】
String username_v = username.getText();
int id = new UserDao2().getMaxId();
//将参数拼接成对象
User u = new User(id+1,username_v,password_v);
//执行插入操作
boolean f = new UserDao2().insert(u);
if(f) {
JOptionPane.showMessageDialog(null,"插入数据成功");
//让注册窗口消失
frame.setVisible(false);
}
else {
JOptionPane.showMessageDialog(null,"插入数据失败");
}
}
});
btnNewButton.setFont(new Font("隶书", Font.BOLD, 25));
btnNewButton.setBounds(288, 244, 178, 51);
frame.getContentPane().add(btnNewButton);
lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(Register.class.getResource("/image/background1.png")));
lblNewLabel.setBounds(10, 10, 561, 388);
frame.getContentPane().add(lblNewLabel);
}
}
这里是把数据库的数据导入
package Dao;
import java.sql.*;
import java.util.ArrayList;
import Model.User;
import Model.User2;
import utility.DBHelper;
public class UserDao2 {
private Connection conn;//数据库连接对象
String sql ;//定义SQL语句
PreparedStatement ps;//预处理表达式对象
ResultSet rs;//查询语句执行后返回的结果集合
public UserDao2() {
super();
// TODO Auto-generated constructor stub
//初始化数据库连接对象
conn = DBHelper.getConnection();
}
//定义方法,执行SQL语句,并处理数据库返回的内容
public ArrayList getAll() {
//定义一个无条件查询的SQL语句
sql = "delect * from tb_user2 ";
//初始化一个容器类数组列表
ArrayList list = new ArrayList();
//产生一个可以执行SQL语句预处理表达式对象
try {
ps = conn.prepareStatement(sql);
//让预处理表达式对象执行查询语句
rs = ps.executeQuery();
//逐行逐字段的拆开结果中的内容
while (rs.next()) {
//拆分结果集中的某一行
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
//将一行数据库的多个字段的值打包放入一个对象当中
User u = new User(id,username,password);
//用一个容器将每次产生的对象保存起来
list.add(u);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//定义插入语句
public boolean insert(User u) {
sql = "insert into tb_user2(id,username,password) valuse(?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, u.getId());
ps.setString(2, u.getUsername());
ps.setString(2, u.getPassword());
//执行插入语句
int i = ps.executeUpdate();
if(i>0) return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//定义删除语句
public boolean delete(int id) {
sql = "delect from tb_user2 where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,id);
int i = ps.executeUpdate();
if(i>0) return false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//定义一个修改语句
public boolean update(User2 u) {
sql = "update tb-user set number=?,category=?,price=?,chandi=? where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, u.getUsername());
ps.setString(1, u.getPassword());
//设置后面两个参数的值
ps.setInt(2, u.getId());
ps.setString(2, u.getUsername());
ps.setString(1, u.getPassword());
int i = ps.executeUpdate();
if(i>0) return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
//定义按条件查询的SQL语句
public ArrayList getUsers(String column,String value) {
sql = "selsect * from tb_user where "+column+" like '%"+value+"%'";
System.out.println("sql:"+sql);
ArrayList list = new ArrayList();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
//逐行逐字段的拆开结果中的内容
while (rs.next()) {
//拆分结果集中的某一行
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
//将一行数据库的多个字段的值打包放入一个对象当中
User u = new User(id,username,password);
//用一个容器将每次产生的对象保存起来
list.add(u);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
//获取该表最大的id值
public int getMaxId() {
sql = "select max(id) as id from tb_user2";
//获取一个可以执行SQL语句的对象
try {
ps = conn.prepareStatement(sql);
//执行SQL语句
rs = ps.executeQuery();
if(rs.next()) {
return rs.getInt("id")+1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 1;
}
//根据用户名和密码判断登录是否成功
public boolean login(String username,String password) {
sql = "select * from tb_user2 where username=? and password=?";
try {
ps = conn.prepareStatement(sql);
//设置问号里面的参数
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if(rs.next())return true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
这是数据库的数据
这是注册页面的图片
请问我报错的地方该怎么修改