创建4个类:StuInfo(实体类)、DBConn(连接、关闭数据库)、DBUtil(数据库操作)、RunMain(测试类)
1.StuInfo实体类:
package com.lykion;
public class StuInfo {
private String sno;
private String sname;
private String dname;
private String ssex;
private int cno;
private double mark;
private String type;
public StuInfo() {
}
public StuInfo(String sno, String sname, String dname, String ssex, int cno, double mark, String type) {
super();
this.sno = sno;
this.sname = sname;
this.dname = dname;
this.ssex = ssex;
this.cno = cno;
this.mark = mark;
this.type = type;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
2.DBConn类,对数据库的操作:打开数据库(连接数据库),操作完成后关闭数据库,释放资源
package com.lykion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn {
private static final String url = "jdbc:mysql://localhost:3306/test"; //数据库地址
private static final String username = "root"; //数据库用户名
private static final String password = "123456"; //数据库密码
private static final String driver = "com.mysql.jdbc.Driver"; //mysql驱动
private static final Connection conn = null;
/**
* 连接数据库
* @return
*/
public static Connection conn() {
Connection conn = null;
try {
Class.forName(driver); //加载数据库驱动
try {
conn = DriverManager.getConnection(url, username, password); //连接数据库
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库链接
* @return
*/
public static void close() {
if(conn != null) {
try {
conn.close(); //关闭数据库链接
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.DBUtil类:主要是数据常用的操作,实例中主要是实现Delete操作
package com.lykion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
/**
* 通过学号sno删除学生信息
* @param sno
*/
public static void Delete(String sno) {
conn = DBConn.conn(); //调用 DBconnection 类的 conn() 方法连接数据库
String sql = "DELETE FROM student01 WHERE sno=?"; //sql语句
try {
ps = conn.prepareStatement(sql);
ps.setString(1, sno);
ps.executeUpdate(); //执行sql语句
System.out.println("删除成功(* ̄︶ ̄");
} catch (SQLException e) {
System.out.println("操作失败o(╥﹏╥");
e.printStackTrace();
}finally {
DBConn.close();
}
}
}
4.RunMain类:测试类,测试DBUtil中的方式是否实现
package com.lykion;
public class RunMain {
public static void main(String[] args) {
String sno = "9006";
DBUtil.Delete(sno);
}
}