基于Java swing和mySQL实现学生信息管理系统:
实现对数据库的增删改查,自己做着玩玩,分享给大家,可以充当个大作业啥的
完整源代码:https://download.csdn.net/download/qq_44864585/12923692
首先,基于Java swing界面做好界面
然后进行数据库的连接,下面是部分源代码
if(ip.equals("")) {
console.append(df.format(new Date())+"\t服务器地址不能为空,请输入服务器地址\n");
JOptionPane.showMessageDialog(null,"请输入服务器地址","提示",JOptionPane.ERROR_MESSAGE);
}
else if(port1.equals("")) {
console.append(df.format(new Date())+"\t端口号不能为空,请输入端口号\n");
JOptionPane.showMessageDialog(null,"请输入端口号","提示",JOptionPane.ERROR_MESSAGE);
}
else if(database1.equals("")) {
console.append(df.format(new Date())+"\t数据库名不能为空,请输入数据库名\n");
JOptionPane.showMessageDialog(null,"请输入数据库名","提示",JOptionPane.ERROR_MESSAGE);
}
else if(user.equals("")) {
console.append(df.format(new Date())+"\t用户名不能为空,请输入用户名\n");
JOptionPane.showMessageDialog(null,"请输入用户名","提示",JOptionPane.ERROR_MESSAGE);
}
else if(password.equals("")) {
console.append(df.format(new Date())+"\t密码不能为空,请输入密码\n");
JOptionPane.showMessageDialog(null,"请输入密码","提示",JOptionPane.ERROR_MESSAGE);
}
else {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e1) {
e1.printStackTrace();
console.append(df.format(new Date())+"\t数据库连接失败\t"+e1.getMessage()+"\n");
JOptionPane.showMessageDialog(null,"驱动不正确,数据库连接失败","提示",JOptionPane.ERROR_MESSAGE);
}
try {
connectUrl ="jdbc:MySQL://"+ip+":"+port1+"/"+database1+"?characterEncoding=utf8";
con = DriverManager.getConnection(connectUrl,user,password); //建立数据库连接
console.append(df.format(new Date())+"\t数据库连接成功\n");
JOptionPane.showMessageDialog(null,"数据库连接成功","提示",JOptionPane.PLAIN_MESSAGE);
closebtn.setEnabled(true);
selectallbtn.setEnabled(true);
getconnectbtn.setEnabled(false);
}
catch(SQLException e1) {
e1.printStackTrace();
console.append(df.format(new Date())+"\t数据库连接失败\t"+e1.getMessage()+"\n");
JOptionPane.showMessageDialog(null,"数据库连接失败","提示",JOptionPane.ERROR_MESSAGE);
}
}
利用SQL语句对数据库进行增删改查操作
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(1, i);
pst.setString(2, s2);
pst.setInt(3,i2);
pst.setString(4, s4);
pst.executeUpdate();
console.append(df.format(new Date())+"\t数据插入成功\n");
history.append(df.format(new Date())+"\tINSERT INTO "+tab+" VALUES ("+s1+","+s2+","+s3+","+s4+");\n");
model.addRow(new String[] {s1+"", s2,s3+"",s4});
String s=iddel.getText();
Statement stmt1 =null;
if(null == s || s.equals("")) {
console.append(df.format(new Date())+"\tid不能为空,请输入id\n");
JOptionPane.showMessageDialog(null,"请输入id","提示",JOptionPane.ERROR_MESSAGE);
}
else if(tab=="") {
console.append(df.format(new Date())+"\t表名不能为空,请输入表名\n");
JOptionPane.showMessageDialog(null,"请输入表名","提示",JOptionPane.ERROR_MESSAGE);
}
else {
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //建立Statement对象
String sqlStr1 = "select * from "+tab+" where id="+s;
history.append(df.format(new Date())+"\tselect from "+tab+" where id="+s+"\n");
rs = stmt.executeQuery(sqlStr1); //执行SQL语句,返回结果集
if(rs.next()) {
stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //建立Statement对象
String sqlStr2 ="delete from "+tab+" where id="+s;
stmt1.executeUpdate(sqlStr2); //执行SQL语句,返回结果集
console.append(df.format(new Date())+"\t删除成功\n");
history.append(df.format(new Date())+"\tdelete from "+tab+" where id="+s+";\n");
JOptionPane.showMessageDialog(null,"删除成功","提示",JOptionPane.PLAIN_MESSAGE);
}
else{
console.append(df.format(new Date())+"\t没有找到你要删除的记录\n");
JOptionPane.showMessageDialog(null,"没有找到你要删除的记录!","提示",JOptionPane.ERROR_MESSAGE);
}