版权声明:本文为博主原创文章,未经博主允许不得转载。
注意:我用的是MySQL数据库。
util包: DbUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DbUtil {
//数据库地址
private static String dbUrl = "jdbc:mysql://localhost:3306/db_testmysql";
//用户名
private static String dbUserName = "root";
//密码
private static String dbPassword = "123456";
//驱动名称
private static String jdbcName = "com.mysql.jdbc.Driver";
//获取数据连接
public Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
return con;
}
//关闭连接
public void close(PreparedStatement pstmt,Connection con) throws Exception{
if(pstmt!=null){
pstmt.close();
if(con!=null){
con.close();
}
}
}
}
student包: Student.java
package student;
public class Student {
// 学生ID
private int id;
// 学生姓名
private String stuName;
// 学生年龄
private int stuAge;
// 学生班级
private int stuClass;
// 学生课程
private String stuLesson;
public Student(String stuName, int stuAge, int stuClass, String stuLesson) {
this.stuName = stuName;
this.stuAge = stuAge;
this.stuClass = stuClass;
this.stuLesson = stuLesson;
}
public Student(int id, String stuName, int stuAge, int stuClass,String stuLesson) {
this.id = id;
this.stuName = stuName;
this.stuAge = stuAge;<