package ms; //新用户注册 import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class New_User extends JFrame{ JLabel tips1,tips2,tips3,tips4; JTextField wbk1,wbk2,wbk3,wbk4; JButton check_btn; JPanel mb1,mb2,mb3,mb4; String username; Connection conn=null; public New_User() { tips1=new JLabel("提示:请输入用户名"); tips2=new JLabel("提示:密码(5位)"); tips3=new JLabel("提示:请输入性别"); tips4=new JLabel("提示:请输入电话"); wbk1=new JTextField(6); wbk2=new JTextField(6); wbk3=new JTextField(6); wbk4=new JTextField(9); check_btn=new JButton("提交"); check_btn.addActionListener(new checkListen()); mb1=new JPanel(); mb2=new JPanel(); mb1.add(wbk1);mb1.add(tips1); mb1.add(wbk2);mb1.add(tips2); mb1.add(wbk3);mb1.add(tips3); mb1.add(wbk4);mb1.add(tips4); mb2.add(check_btn,BorderLayout.NORTH); mb1.setLayout(new GridLayout(4,2)); this.add(mb1,BorderLayout.CENTER);this.add(mb2,BorderLayout.SOUTH); this.setVisible(true); this.setSize(350,200); this.setLocationRelativeTo(null);//居中显示 } class checkListen implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 IDB(); pipei(); if(wbk1.getText().equals("")||wbk2.getText().equals(""))//判断文本框是否为空一定要用equals {JOptionPane.showMessageDialog(null, "用户名和密码不能为空!");} else if(username!=null) { JOptionPane.showMessageDialog(null, "用户名已被注册!"); } else{ charu(); JOptionPane.showMessageDialog(null, "注册成功!"); } } } //连接数据库 public void IDB() { try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url="jdbc:sqlserver://localhost:1433;DatabaseName=大学城校园美食联盟"; String user="sa"; String passWord="lch"; conn=DriverManager.getConnection(url,user,passWord); }catch(Exception e){ e.printStackTrace(); } } //读取数据库里相应的 public void pipei() { try{ String usname = wbk1.getText(); Statement state = conn.createStatement(); username=null;//记得初始化 String sql = "select username from login where username = '"+usname+"'"; ResultSet res = state.executeQuery(sql); while(res.next()) { username = res.getString("username"); } //conn.close(); }catch(Exception e){ e.printStackTrace(); } } //添加数据 public void charu() { String uname=wbk1.getText(); String psword=wbk2.getText(); String sex=wbk3.getText(); String phone=wbk4.getText(); try{ Statement statement=conn.createStatement(); statement.execute("insert into login values('"+uname+"','"+psword+"','"+sex+"','"+phone+"')"); conn.close(); }catch(Exception e){ } } }

Demo1111 LV30
2021年12月12日
tiashishiwo LV5
2021年7月3日
fdsvsfds LV2
2021年3月16日
此刀名为秋水 LV1
2021年2月20日
freedom2017 LV14
2020年6月2日
狗比lc LV1
2020年1月2日
hanyuiqng LV9
2019年12月24日
啊浩啊 LV7
2019年9月19日
CJ6872400 LV1
2019年7月27日
mangen LV1
2019年6月25日

就爱吃饺子啊 LV1
2024年10月21日
aaaaaaaaa4 LV1
2024年9月19日
ZALZXB20030525
2024年7月1日
暂无贡献等级
18306681497 LV1
2024年6月27日
cx20031221
2024年6月5日
暂无贡献等级
林灵123456 LV8
2024年6月3日
2860615178 LV4
2024年5月29日
R8sixsix LV1
2024年2月27日
廖业贵 LV18
2024年2月23日
adyxhss LV3
2023年12月25日