增删查改的大概安排
写增删查改分别 由三张表组成 ,由学生表,教师表,班级表, 有util entity dao biz Servlet 等包
第一步写util包先连接数据库里
面有DBAccess.java
/**
* 提供了一组获得或关闭数据库对象的方法
*
*/
public class DBAccess {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
InputStream is = DBAccess.class
.getResourceAsStream("config.properties");
Properties properties = new Properties();
properties.load(is);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获得数据连接对象
*
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(PreparedStatement ps) {
if (null != ps) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
close(rs);
close(ps);
close(con);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.cj.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBAccess.getConnection();
DBAccess.close(conn);
System.out.println("isOracle:" + isOracle());
System.out.println("isSQLServer:" + isSQLServer());
System.out.println("isMysql:" + isMysql());
System.out.println("数据库连接(关闭)成功");
}
}
连接数据库还需要config.properties:
#mysql5
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
user=root
pwd=123
第二步定义一个Student实体类
**
public class student {
private int sid;
private String sname;
private String sjy;
private String sbj;
private String sah;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSjy() {
return sjy;
}
public void setSjy(String sjy) {
this.sjy = sjy;
}
public String getSbj() {
return sbj;
}
public void setSbj(String sbj) {
this.sbj = sbj;
}
public String getSah() {
return sah;
}
public void setSah(String sah) {
this.sah = sah;
}
public student() {}
public student(int sid, String sname, String sjy, String sbj, String sah) {
this.sid = sid;
this.sname = sname;
this.sjy = sjy;
this.sbj = sbj;
this.sah = sah;
}
public student(String sname, String sjy, String sbj, String sah) {
this.sname = sname;
this.sjy = sjy;
this.sbj = sbj;
this.sah = sah;
}
}
后面还需要教师实体类,和班级实体类
public class sbj {
private int bid;
private String bname;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public sbj(int bid, String bname) {
super();
this.bid = bid;
this.bname = bname;
}
public sbj() {}
}
public class sjy {
private int jid;
private String jname;
private String jsex;
public int getJid() {
return jid;
}
public void setJid(int jid) {
this.jid = jid;
}
public String getJname() {
return jname;
}
public void setJname(String jname) {
this.jname = jname;
}
public String getJsex() {
return jsex;
}
public void setJsex(String jsex) {
this.jsex = jsex;
}
public sjy() {}
public sjy(int jid, String jname, String jsex) {
super();
this.jid = jid;
this.jname = jname;
this.jsex = jsex;
}
public sjy(String jname, String jsex) {
this.jname = jname;
this.jsex = jsex;
}
}
第三步写dao包
先写一个student接口
public interface IbjDao {
public List<sbj> getAll();
public List<sbj> getAll(String colName,String str);
public sbj getAllByBid(String bid);
public int addbj(sbj s);
public int deletebj(String bid);
public int updatebj(sbj s,String bid);
}
在写一个student的dao方法
public class StuDao implements IstuDao {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
@Override
public Li