2024.3.13
第七天
所花时间:2小时
代码量:400+
博客量:1
了解到的知识点:今天把开学考试的测试代码重新看了一下,首先是连接数据库的代码和bean文件部分,明天更新计划申请的dao层和servlet
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
public static String db_url = "jdbc:mysql://localhost:3306/tb_user?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true";
public static String db_user = "root";
public static String db_pass = "ADGJL12345syl";
public static Connection getConn () {
Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
conn = DriverManager.getConnection(db_url, db_user, db_pass);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @param state
* @param conn
*/
public static void close (Statement state, Connection conn) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close (ResultSet rs, Statement state, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws SQLException {
Connection conn = getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql ="select * from tb_user.tiaoji";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("空");
}else{
System.out.println("不空");
}
sql ="select * from tb_user.jihua";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("空");
}else{
System.out.println("不空");
}
}
}
package bean; public class Jihua { private String zhuanye; private int number; private int zongfen; private int zhengzhi; private int yingyu; private int shuxue; private String time; public Jihua(String zhuanye, int number, int zongfen, int zhengzhi, int yingyu, int shuxue, String time) { this.zhuanye = zhuanye; this.number = number; this.zongfen = zongfen; this.zhengzhi = zhengzhi; this.yingyu = yingyu; this.shuxue = shuxue; this.time = time; } public Jihua() { } public String getZhuanye() { return zhuanye; } public void setZhuanye(String zhuanye) { this.zhuanye = zhuanye; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public int getZongfen() { return zongfen; } public void setZongfen(int zongfen) { this.zongfen = zongfen; } public int getZhengzhi() { return zhengzhi; } public void setZhengzhi(int zhengzhi) { this.zhengzhi = zhengzhi; } public int getYingyu() { return yingyu; } public void setYingyu(int yingyu) { this.yingyu = yingyu; } public int getShuxue() { return shuxue; } public void setShuxue(int shuxue) { this.shuxue = shuxue; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }
package bean; public class login { public char id; public char password; public login(char id, char password) { this.id = id; this.password = password; } public login() { } public char getId() { return id; } public void setId(char id) { this.id = id; } public char getPassword() { return password; } public void setPassword(char password) { this.password = password; } }
package bean; public class Tiaoji { private String name; private String id; private String zhuanye; private String zongfen; private String sizhang; private String yingyu; private String shuxue; private String zonghe; private String jieguo; public Tiaoji(String name, String id, String zhuanye, String zongfen, String sizhang, String yingyu, String shuxue, String zonghe, String jieguo) { this.name = name; this.id = id; this.zhuanye = zhuanye; this.zongfen = zongfen; this.sizhang = sizhang; this.yingyu = yingyu; this.shuxue = shuxue; this.zonghe = zonghe; this.jieguo = jieguo; } public Tiaoji() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getZhuanye() { return zhuanye; } public void setZhuanye(String zhuanye) { this.zhuanye = zhuanye; } public String getZongfen() { return zongfen; } public void setZongfen(String zongfen) { this.zongfen = zongfen; } public String getSizhang() { return sizhang; } public void setSizhang(String sizhang) { this.sizhang = sizhang; } public String getYingyu() { return yingyu; } public void setYingyu(String yingyu) { this.yingyu = yingyu; } public String getShuxue() { return shuxue; } public void setShuxue(String shuxue) { this.shuxue = shuxue; } public String getZonghe() { return zonghe; } public void setZonghe(String zonghe) { this.zonghe = zonghe; } public String getJieguo() { return jieguo; } public void setJieguo(String jieguo) { this.jieguo = jieguo; } }