1、在数据库jdbc中建立表teach,如下;
2、初始界面
3.1、按姓名查找(number为主键)
3.2、按学号进行删除(建议先查询再删除,也可直接输入学号删除)
3.3、增加记录(学号不可重复(主键))
3.4、修改记录
注意:本小应用未对异常进行处理,需要进行深一步优化,支持初学者对知识巩固。
代码如下
1:
package studentManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JDBCUtils {
private static String url = "jdbc:mysql://127.0.0.1:3306/jdbc?"
+ "useUnicode=true&characterEncoding=utf-8&useSSL=false";
private static String user = "root";
private static String password = "user182872";
private JDBCUtils() {}
static {// 静态代码只会执行一次
// 1.加载驱动,
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2:
package studentManager;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class StudentManager {
private JFrame frame;
private JTextField nameT;
private JTextField cT;
private JTextField mT;
private JTextField classT;
private JTextField eT;
private JTextField numberT;
private JTextArea info;
private Connection conn = null;
private java.sql.PreparedStatement ps = null;
private ResultSet rs = null;
private String name;
private String number;
private String team;
private String chinese;
private String math;
private String english;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentManager window = new StudentManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public StudentManager() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 433, 303);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u59D3\u540D");
label.setBounds(23, 22, 54, 15);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u5B66\u53F7");
label_1.setBounds(102, 22, 54, 15);
frame.getContentPane().add(label_1);
JLabel label_2 = new JLabel("\u73ED\u7EA7");
label_2.setBounds(174, 22, 34, 15);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("\u8BED\u6587");
label_3.setBounds(231, 22, 54, 15);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("\u6570\u5B66");
label_4.setBounds(289, 22, 54, 15);
frame.getContentPane().add(label_4);
JLabel label_5 = new JLabel("\u82F1\u8BED");
label_5.setBounds(357, 22, 54, 15);
frame.getContentPane().add(label_5);
nameT = new JTextField();
nameT.setBounds(10, 47, 56, 24);
frame.getContentPane().add(nameT);
cT = new JTextField();
cT.setBounds(228, 47, 41, 24);
frame.getContentPane().add(cT);
mT = new JTextField();
mT.setBounds(288, 47, 41, 24);
frame.getContentPane().add(mT);
eT = new JTextField();
eT.setBounds(353, 47, 41, 24);
frame.getContentPane().add(eT);
classT = new JTextField();
classT.setBounds(167, 47, 41, 24);
frame.getContentPane().add(classT);
numberT = new JTextField();
numberT.setBounds(89, 47, 56, 24);
frame.getContentPane().add(numberT);
JButton addB = new JButton("\u589E");
addB.addActionListener(new Handle() {
public void actionPerformed(ActionEvent e) {
try {
get();
add();
info.setText("插入成功!");
// set();
} catch (SQLException e1) {
// e1.printStackTrace();
info.setText("插入失败!请确认学号是否重复?");
}
}
});
addB.setBounds(10, 101, 69, 23);
frame.getContentPane().add(addB);
JButton deleteB = new JButton("\u5220");
deleteB.addActionListener(new Handle() {
public void actionPerformed(ActionEvent e) {
try {
delete();
clc();
info.setText("删除成功!");
} catch (SQLException e1) {
// e1.printStackTrace();
info.setText("删除失败!请确认该记录是否存在?");
}
}
});
deleteB.setBounds(110, 101, 69, 23);
frame.getContentPane().add(deleteB);
JButton lookB = new JButton("\u67E5");
lookB.addActionListener(new Handle() {
public void actionPerformed(ActionEvent e) {
try {
get();
look();
set();
info.setText("查询成功!");
} catch (SQLException e1) {
// e1.printStackTrace();
info.setText("查询失败!请确认是否以输入姓名进行查询?");
}
}
});
lookB.setBounds(216, 101, 69, 23);
frame.getContentPane().add(lookB);
JButton changeB = new JButton("\u6539");
changeB.addActionListener(new Handle() {
public void actionPerformed(ActionEvent e) {
try {
get();
update();
set();
info.setText("修改成功!");
} catch (SQLException e1) {
// e1.printStackTrace();
info.setText("修改失败!");
}
}
});
changeB.setBounds(318, 101, 69, 23);
frame.getContentPane().add(changeB);
JButton clcB = new JButton("\u6E05\u7A7A");
clcB.addActionListener(new Handle() {
public void actionPerformed(ActionEvent e) {
clc();
}
});
clcB.setBounds(301, 231, 93, 23);
frame.getContentPane().add(clcB);
info = new JTextArea();
info.setBounds(10, 156, 384, 65);
frame.getContentPane().add(info);
JButton exitB = new JButton("\u9000\u51FA");
exitB.addActionListener(new Handle() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
exitB.setBounds(10, 231, 93, 23);
frame.getContentPane().add(exitB);
}
private void look() throws SQLException {
try {
conn = JDBCUtils.getConnection();
String sql1 = "select * from teach where name=?";
ps = conn.prepareStatement(sql1);
ps.setString(1, name);
rs = ps.executeQuery();
while (rs.next()) {
read();
}
} finally {
rs.close();
ps.close();
conn.close();
}
}
private void add() throws SQLException {
try {
conn = JDBCUtils.getConnection();
String sql1 = "insert into teach values(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql1);
setStringOne();
} finally {
rs.close();
ps.close();
conn.close();
}
}
private void delete() throws SQLException {
try {
conn = JDBCUtils.getConnection();
String sql1 = "delete from teach where number = ?";
ps = conn.prepareStatement(sql1);
ps.setInt(1, Integer.parseInt(number));
ps.executeUpdate();
} finally {
rs.close();
ps.close();
conn.close();
}
}
private void update() throws SQLException {
try {
conn = JDBCUtils.getConnection();
String sql1 = "update teach set name = ? , class = ? , Chinese = ?, Math = ? , English = ? where number = ?";
ps = conn.prepareStatement(sql1);
setStringTow();
} finally {
rs.close();
ps.close();
conn.close();
}
}
private void read() throws SQLException {
name = rs.getString(1);
number = rs.getString(2);
team = rs.getString(3);
chinese = rs.getString(4);
math = rs.getString(5);
english = rs.getString(6);
System.out.println(name + "\t" + number + "\t" + team + "\t" + chinese + "\t" + math + "\t" + english);
}
private void get() {
name = nameT.getText().trim();
number = numberT.getText().trim();
team = classT.getText().trim();
chinese = cT.getText().trim();
math = mT.getText().trim();
english = eT.getText().trim();
}
private void set() {
nameT.setText(name);
numberT.setText(number + "");
classT.setText(team);
cT.setText(chinese + "");
mT.setText(math + "");
eT.setText(english + "");
}
private void setStringOne() throws SQLException {
ps.setString(1, name);
ps.setInt(2, Integer.parseInt(number));
ps.setString(3, team);
ps.setInt(4, Integer.parseInt(chinese));
ps.setInt(5, Integer.parseInt(math));
ps.setInt(6, Integer.parseInt(english));
ps.executeUpdate();
}
private void setStringTow() {
try {
ps.setString(1, name);
ps.setString(2, team);
ps.setInt(3, Integer.parseInt(chinese));
ps.setInt(4, Integer.parseInt(math));
ps.setInt(5, Integer.parseInt(english));
ps.setInt(6, Integer.parseInt(number));
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void clc() {
nameT.setText(" ");
numberT.setText(" ");
classT.setText(" ");
cT.setText(" ");
mT.setText(" ");
eT.setText(" ");
}
class Handle implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
}
}
}