马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 guhusf 于 2021-12-25 18:59 编辑 package 复习;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import java.util.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
class lx implements ActionListener,ListSelectionListener{
public JFrame f =null;
public JPanel p = null;
public JButton bz,bs,bg,bc = null;
public JTable t = null;
public DefaultTableModel dtm = null;
public JScrollPane jp = null;
Vector vt = new Vector();
Vector vd = new Vector();
lx(){
f = new JFrame("kk");
p = new JPanel();
bz = new JButton("增");
bs = new JButton("删");
bg = new JButton("改");
bc = new JButton("查");
vt.add("ID");vt.add("name");vt.add("java");vt.add("C");
dtm = new DefaultTableModel();
t = new JTable(dtm);
jp = new JScrollPane(t);
p.setLayout(new GridLayout(1,4));
p.add(bz);p.add(bs);p.add(bg);p.add(bc);
f.setLayout(new BorderLayout());
f.add(jp,BorderLayout.CENTER);
f.add(p,BorderLayout.SOUTH);
bz.addActionListener(this);
bs.addActionListener(this);
bg.addActionListener(this);
bc.addActionListener(this);
t.getSelectionModel().addListSelectionListener(this);
f.setSize(400,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Shujuku.connect();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Shujuku.close();
System.exit(0);
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new lx();
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==bz) {
Scanner sc = new Scanner(System.in);
String ID,name,java,C;
System.out.println("请输入学号:");
ID = sc.nextLine();
System.out.println("请输入姓名:");
name = sc.nextLine();
System.out.println("请输入Java成绩:");
java = sc.nextLine();
System.out.println("请输入C语言成绩:");
C = sc.nextLine();
String sql = "insert into score(ID,name,java,C) values('"+ID+"','"+name+"','"+java+"','"+C+"')";
Shujuku.Update(sql);
Vector<String> row = new Vector<String>();
row.add(ID);row.add(name);row.add(java);row.add(C);
dtm.addRow(row);
}
if(e.getSource()==bc) {
String sql = "select * from score";
ResultSet rs = Shujuku.query(sql);
try {
while(rs.next()) {
Vector row = new Vector<String>();
row.add(rs.getString(1));
row.add(rs.getString(2));
row.add(rs.getString(3));
row.add(rs.getString(4));
vd.add(row);
}
dtm.setDataVector(vd,vt);
}catch(SQLException e1) {
e1.printStackTrace();
}
}
if(e.getSource()==bs) {
int row = t.getSelectedRow();
String id = (String)t.getValueAt(row, 0);
String sql = "delete from score where ID ='"+id+"'";
Shujuku.Update(sql);
dtm.removeRow(row);
}
if(e.getSource()==bg) {
String ID,sql = null;
int row = t.getSelectedRow();
int col = t.getSelectedColumn();
System.out.println("请输入修改后的值");
Scanner sc = new Scanner(System.in);
String x = sc.nextLine();
if(col==0) {
ID = (String)t.getValueAt(row, col);
sql = "update score set ID = '"+x+"'where ID = '"+ID+"'";
}
else {
ID = (String)t.getValueAt(row, 0);
if(col == 1) {
sql = "update score set name='"+x+"'where ID = '"+ID+"'";
}
else if(col == 2) {
sql = "update score set java = '"+x+"'where ID = '"+ID+"'";
}
else if (col == 3) {
sql = "update score set C = '"+x+"'where ID= '"+ID+"'";
}
t.setValueAt(x, row, col);
Shujuku.Update(sql);
}
}
}
}
class Shujuku{
public static Connection coon = null;
public static Statement stmt = null;
public static ResultSet rs = null;
public static Connection connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String Url = "jdbc:mysql://localhost:3306/student";
String User = "root";
String Password = "010828";
coon = DriverManager.getConnection(Url,User,Password);
System.out.println("操作成功");
return coon;
}catch(ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return null;
}
public static ResultSet query(String sql) {
// TODO Auto-generated method stub
try {
coon = connect();
stmt = coon.createStatement();
rs = stmt.executeQuery(sql);
}catch(SQLException e2) {
e2.printStackTrace();
}
return rs;
}
public static void Update(String sql) {
// TODO Auto-generated method stub
try {
coon = connect();
stmt = coon.createStatement();
stmt.execute(sql);
}catch(SQLException e) {
e.printStackTrace();
}
}
public static void close() {
// TODO Auto-generated method stub
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (coon != null)
coon.close();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
}
}
|