package pkg;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class 第三题 {
static Connection con;
JButton jb1,jb2,jb3;
JTextField jtf1,jtf2,jtf3,jtf4;
JRadioButton nan,nv;
public Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动加载成功");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:sqlserver:"
+ "//127.0.0.1:1433;DatabaseName=aaa","sa","123456");
System.out.println("数据库连接成功");
}catch(SQLException e) {
e.printStackTrace();
}
return con;
}
第三题() {
JFrame jf = new JFrame("学生信息录入系统");
JPanel jp = new JPanel();
jp.setBackground(Color.LIGHT_GRAY);
String[] jlName = {"学号","姓名","性别","出生日期","籍贯"};
Box box = Box.createHorizontalBox();
Box box1 = Box.createVerticalBox();
Box box2 = Box.createVerticalBox();
Box box3 = Box.createHorizontalBox();
Box box4 = Box.createHorizontalBox();
for(int i = 0;i<jlName.length;i++) {
JLabel jl = new JLabel(jlName[i]);
box1.add(Box.createVerticalStrut(12));
box1.add(jl);
}
box1.add(Box.createHorizontalStrut(40));
nan = new JRadioButton("男");
nan.setBackground(Color.cyan);
nv = new JRadioButton("女");
nv.setBackground(Color.PINK);
ButtonGroup bg = new ButtonGroup();
box3.add(nan);box3.add(Box.createHorizontalStrut(30));
box3.add(nv);
bg.add(nan);bg.add(nv);
JLabel jl1 = new JLabel("注意:出生日期输入格式为YYYY/MM/DD");
jtf1 = new JTextField(10);
jtf2 = new JTextField(10);
jtf3 = new JTextField(10);
jtf4 = new JTextField(12);
box2.add(Box.createHorizontalStrut(20));
box2.add(Box.createVerticalStrut(10));
box2.add(jtf1); box2.add(Box.createVerticalStrut(8));
box2.add(jtf2); box2.add(Box.createVerticalStrut(5));
box2.add(box3);
box2.add(jtf3); box2.add(jl1);
box2.add(jtf4);
jb1 = new JButton("存入");
jb2 = new JButton("查询");
jb3 = new JButton("删除此id");
box4.add(jb1);box4.add(jb2);box4.add(jb3);
jb1.addActionListener(new jbListener1());
jb2.addActionListener(new jbListener2());
jb3.addActionListener(new jbListener3());
box2.add(box4);
box.add(box1);
box.add(Box.createHorizontalStrut(20));
box.add(box2);
jp.add(box);
jf.add(jp);
jf.setBounds(300, 300, 500, 300);
jf.setVisible(true);
}
class jbListener1 implements ActionListener{
String data1,data2,data3,data4,data5;
String sql;Statement stmt;
public void actionPerformed(ActionEvent e) {
data1 = jtf1.getText();
data2 = jtf2.getText();
if(nan.isSelected()) {
data3 = "男";
}else {
data3 = "女";
}
data4 = jtf3.getText();
data5 = jtf4.getText();
try {
String sql = "insert into stu values(?,?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, data1);
pstmt.setString(2, data2);
pstmt.setString(3, data3);
pstmt.setString(4, data4);
pstmt.setString(5, data5);
pstmt.executeUpdate();
System.out.println("添加完毕");
}catch(Exception e1) {
e1.printStackTrace();
}
}
}
class jbListener2 implements ActionListener{
String sql;Statement stmt;
public void actionPerformed(ActionEvent e) {
try {
sql = "select*from stu";
stmt = con.createStatement();//sql语句对象
ResultSet res = stmt.executeQuery(sql);
while(res.next()) {
String id = res.getString("id");
String name = res.getString("name");
String sex = res.getString("sex");
String birthday = res.getString("birthday");
String place = res.getString("place");
System.out.println("编号:"+id+"姓名:"+name+"性别:"+sex+"生日:"+birthday+"地址:"+place+'\n');
}
System.out.println("查询完毕");
}catch(Exception e1) {
e1.printStackTrace();
}
}
}
class jbListener3 implements ActionListener{
String sql;PreparedStatement ps;
public void actionPerformed(ActionEvent e) {
try {
String ID;
ID = jtf1.getText();
sql ="delete from stu where id=?";
ps = con.prepareStatement(sql);
ps.setString(1, ID);
ps.executeUpdate();
System.out.println("删除完毕");
}catch(Exception e1) {
e1.printStackTrace();
}
}
}
public static void main(String[]args) {
第三题 c = new 第三题();
c.getConnection();
}
}