📄 b_insert.java
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.sql.*;
import java.util.LinkedList;
class B_insert extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4;
JButton b1,b2;
JTextField t1,t2,t3,t4;
Connection con;
Statement sta;
String conURL="jdbc:mysql://localhost/login?user=root&password=root";
ResultSet rs;
B_insert()
{
super("学生信息添加");
setBounds(200,200,400,300);
JPanel p=new JPanel();
p.setBounds(230,230,340,240);
JPanel p1=new JPanel();
l1=new JLabel("姓名: (符号跟数字组成)");
l2=new JLabel("学号: (数字组成001~999)");
l3=new JLabel("性别: (男或者女)");
l4=new JLabel("所在宿舍: (真实的宿舍)");
t1=new JTextField();
t2=new JTextField();
t3=new JTextField();
t4=new JTextField();
p1.setLayout(new GridLayout(8,1));
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(t2);
p1.add(l3);p1.add(t3);
p1.add(l4);p1.add(t4);
JPanel p2=new JPanel();
b1=new JButton("提交");
b2=new JButton("重置");
p2.setLayout(new GridLayout(1,2));
p2.add(b1);p2.add(b2);
p.setLayout(new BorderLayout());
p.add(p1,"Center");
p.add(p2,"South");
setLayout(new BorderLayout());
add(p,"Center");
this.setResizable(false);
ConSql();
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1)
{
insert();
}
else if(e.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
}
public void insert()
{
String s1=t1.getText().trim();
String s2=t2.getText().trim();
String s3=t3.getText().trim();
String s4=t4.getText().trim();
String temp="insert into student values('"+s1+"','"+s2+"','"+s3+"','"+s4+"')";
try{
rs=sta.executeQuery("select d_number,d_count,d_true from dormitory");
int count=0;
String s;
boolean b1=false;
while(rs.next())
{
s=rs.getString("d_number").trim();
count=rs.getInt("d_count");
System.out.println(s);
if(s.equals(s4))
{
b1=true;
if(count<4)
{
sta.executeUpdate(temp);
PreparedStatement ps=con.prepareStatement("update dormitory set d_count=? where d_number=?");
count++;
ps.setInt(1, count);
ps.setString(2, ""+s4+"");
ps.executeUpdate();
this.dispose();
new JOptionPane().showMessageDialog(null, "添加成功");
ps.close();
rs.close();
sta.close();
con.close();
break;
}
else{
this.dispose();
new JOptionPane().showMessageDialog(null, "宿舍满了");
rs.close();
sta.close();
con.close();
break;
}
}
else
{
continue;
}
}
if(!b1)
{
this.dispose();
new JOptionPane().showMessageDialog(null, "宿舍不存在");
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void ConSql()
{
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(conURL);
sta=con.createStatement();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -