📄 gerenxinxi.java
字号:
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.net.*;
public class gerenxinxi extends JFrame implements ActionListener
{
Connection con;
ResultSet rs;
Statement st;
JTextField text1,text2,text3,text4,text5,text6,text7;
JButton button1,button2;
String sid1=new String();
public void gerenxinxi()
{
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
text4=new JTextField(10);
text5=new JTextField(10);
text6=new JTextField(10);
text7=new JTextField(10);
button1=new JButton("注册");
button1.addActionListener(this);
button2=new JButton("退出");
button2.addActionListener(this);
Box box1=Box.createHorizontalBox();
box1.add(new JLabel("学生个人信息注册",JLabel.CENTER));
Box box2=Box.createHorizontalBox();
box2.add(new JLabel("学号:",JLabel.CENTER));
box2.add(text1);
Box box3=Box.createHorizontalBox();
box3.add(new JLabel("姓名:",JLabel.CENTER));
box3.add(text2);
Box box4=Box.createHorizontalBox();
box4.add(new JLabel("性别:",JLabel.CENTER));
box4.add(text3);
Box box5=Box.createHorizontalBox();
box5.add(new JLabel("年龄:",JLabel.CENTER));
box5.add(text4);
Box box6=Box.createHorizontalBox();
box6.add(new JLabel("电话:",JLabel.CENTER));
box6.add(text5);
Box box7=Box.createHorizontalBox();
box7.add(new JLabel("住址:",JLabel.CENTER));
box7.add(text6);
Box box8=Box.createHorizontalBox();
box8.add(new JLabel("班级:",JLabel.CENTER));
box8.add(text7);
Box boxH=Box.createVerticalBox();
boxH.add(box1);
boxH.add(box2);
boxH.add(box3);
boxH.add(box4);
boxH.add(box5);
boxH.add(box6);
boxH.add(box7);
boxH.add(box8);
boxH.add(Box.createVerticalGlue());
JPanel pCenter=new JPanel();
pCenter.add(boxH);
setLayout(new BorderLayout());
add(pCenter,BorderLayout.CENTER);
JPanel pSouth=new JPanel();
pSouth.add(button1);
pSouth.add(button2);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);
setBounds(100,50,300,300);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
try{//连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e2)
{
System.out.println(""+e2);
}
try{
con=DriverManager.getConnection("jdbc:odbc:lhm","","");
st=con.createStatement();
rs=st.executeQuery("Select * FROM xinxi");
}
catch(SQLException el){}//连接数据库
int count=0;
String sid=new String();
sid=text1.getText();
String sname=new String();
sname=text2.getText();
String ssex=new String();
ssex=text3.getText();
String sage=new String();
sage=text4.getText();
String stel=new String();
stel=text5.getText();
String saddress=new String();
saddress=text6.getText();
String sclass=new String();
sclass=text7.getText();
try{
while(rs.next())//判断用户名是否存在
{
sid1=rs.getString("studentid");
if(sid.equals(sid1)){count++;}
}}catch(Exception ee){}
if(count>0){JOptionPane.showMessageDialog(null,"数据库中已存在该学号,请查询后在注册!");
text1.setText("");
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
text7.setText("");}
else if(sid.length()==0||sname.length()==0||ssex.length()==0||sage.length()==0||stel.length()==0||saddress.length()==0||sclass.length()==0)
{JOptionPane.showMessageDialog(null,"请完整填写个人信息!");}
else
{String strSQL6 ="insert into xinxi(studentid,studentname,studentsex,studentage,studenttel,studentaddress,studentclass) values ('"+sid + "','" + sname + "','" + ssex + "','" + sage + "','" + stel + "','" + saddress + "','" + sclass+"')";
try
{
st.executeUpdate(strSQL6);
}
catch(Exception exx){}
JOptionPane.showMessageDialog(null,"注册成功!");
try//关闭数据库
{
st.close();
con.close();
}
catch(SQLException e3 )
{
JOptionPane.showMessageDialog(null,"数据库关闭失败!");
}
}
}
if(e.getSource()==button2)
{setVisible(false);}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -