這個類是這個課程設計中的一個類,非獨立運行(但是測試連接數據庫可以獨立運行,main方法和構造函數已經註釋,需要測試把註釋去掉)。
使用用法:如果配置不變,即數據庫爲Mysql,在本地上並且端口爲3306(連接需要下載JDBC mysql的連接包),登錄用戶名爲ROOT,沒有密碼。並且數據庫名稱爲blogcheckdb,如果有改變可以使用set方法改變。使用時使用connect方法連接數據庫。
連接需要使用數據庫,稍後給出數據庫文件。注意,沒有數據庫請 不要連接
本類按照需求寫了非常多的方法用作接口,詳細使用請看註釋。
import java.sql.*;
public class DBC {
Connection con;// 連接
Statement stmt;
ResultSet rs;// 返回值
String ip = "127.0.0.1";
String port = "3306";
String DBname = "blogcheckdb";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + DBname
+ "?characterEncoding=utf8";// 數據庫鏈接
String user = "root";// 數據庫用戶名
String password = null;// 數據庫密碼
/*public DBC() throws SQLException {
this.connect();
try {// 連接JDBC驅動
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ce) {
System.out.println("SqlException:" + ce.getMessage());
System.out.println("加載驅動失敗");
}
con = DriverManager.getConnection(url, user, password);
stmt = con.createStatement();
// this.connect();
// this.selectnews();
// this.deleteclass(13);
// this.insertnews(1, 1552, 122, 0, 9, 0, 0, 17, true);
// this.updatanews(9, 1552, 122, 1, 9, 1, 1, 19, true);
// this.selectclass();
// this.selectallstudent();
// this.insertclass("網絡121");
// this.closeDB();
}*/
public void connect() {
try {// 連接JDBC驅動
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ce) {
System.out.println("SqlException:" + ce.getMessage());
System.out.println("加載驅動失敗");
}
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt = con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* public static void main(String[] args) throws SQLException {
new DBC();
}*/
public void insertclass(String classname) {// 插入班級
try {
int a = stmt.executeUpdate("insert into class(classname) values('"
+ classname + "')");
if (a != 0)
System.out.println("插入班級" + classname + "成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertstudents(String studentname, String blogurl, int classno) {// 插入學生
try {
int a = stmt
.executeUpdate("insert into students(studentname,url,classno) values('"
+ studentname
+ "','"
+ blogurl
+ "',"
+ classno
+ ")");
if (a != 0)
System.out.println("插入學生" + studentname + "成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[][] selectallstudent() {// 查詢所有學生號碼和姓名和URL,0爲學生名,1學號,2爲url
int studentcount;// 人數
String[][] Studentname = null;
try {
rs = stmt.executeQuery("SELECT count(*) count FROM Students");
rs.next();
studentcount = Integer.parseInt(rs.getString("count"));
Studentname = new String[3][studentcount];
rs = stmt.executeQuery("select * FROM students");
// rs.next();
for (int i = 0; i < studentcount && rs.next(); i++) {
Studentname[0][i] = rs.getString("studentname");
Studentname[1][i] = rs.getString("studentno");
Studentname[2][i] = rs.getString("url");
// System.out.print(Studentname[0][i]);
// System.out.print(Studentname[1][i]);
// System.out.println(Studentname[2][i]);
}
// System.out.println(studentcount);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Studentname;
}
public String[][] selectclassstudent(int classno) {// 查詢班級內學生,0爲學生名,1學號,2爲url
int studentcount;// 人數
String[][] Student = null;
try {
rs = stmt
.executeQuery("SELECT count(*) count FROM Students where classno ='"
+ classno + "'");
rs.next();
studentcount = Integer.parseInt(rs.getString("count"));
Student = new String[3][studentcount];
rs = stmt.executeQuery("select * FROM students");
for (int i = 0; i < studentcount && rs.next(); i++) {
Student[0][i] = rs.getString("studentname");
Student[1][i] = rs.getString("studentno");
Student[2][i] = rs.getString("url");
}
// System.out.println(studentcount);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Student;
}
public String[][] selectclass() {// 查詢班級,0爲班級名稱,1爲班級號碼
int classcount;// 人數
String[][] classm = null;
try {
rs = stmt.executeQuery("SELECT count(*) count FROM class");
rs.next();
classcount = Integer.parseInt(rs.getString("count"));
classm = new String[2][classcount];
rs = stmt.executeQuery("select * FROM class");
// rs.next();
for (int i = 0; i < classcount && rs.next(); i++) {
classm[0][i] = rs.getString("classname");
classm[1][i] = rs.getString("classno");
// Studentname[2][i] = rs.getString("url");
System.out.print(classm[0][i]);
System.out.print(classm[1][i]);
// System.out.println(Studentname[2][i]);
}
// System.out.println(studentcount);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return classm;
}
public String[][] selectnews() {// 查詢更新狀態,0爲學生名,1學號,2爲url
int count;// 人數
String[][] classm = null;
try {
rs = stmt
.executeQuery("SELECT count(*) count FROM students a ,news b where a.studentno = b.studentno AND b.new = TRUE");
rs.next();
count = Integer.parseInt(rs.getString("count"));
classm = new String[3][count];
rs = stmt
.executeQuery("SELECT studentname,url,a.studentno FROM students a ,news b where a.studentno = b.studentno AND b.new = TRUE");
// rs.next();
for (int i = 0; i < count && rs.next(); i++) {
classm[0][i] = rs.getString("studentname");
classm[1][i] = rs.getString("studentno");
classm[2][i] = rs.getString("url");
// Studentname[2][i] = rs.getString("url");
System.out.print(classm[0][i]);
System.out.print(classm[1][i]);
System.out.println(classm[2][i]);
}
// System.out.println(studentcount);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return classm;
}
public void deleteclass(int classno) {// 刪除班級
try {
rs = stmt
.executeQuery("SELECT count(*) count FROM students where classno = "
+ classno);
rs.next();
int count = Integer.parseInt(rs.getString("count"));
int student[] = new int[count];
rs = stmt
.executeQuery("SELECT studentno FROM students where classno = "
+ classno);
for (int i = 0; i < count; i++) {
rs.next();
student[i] = Integer.parseInt(rs.getString("studentno"));
}
for (int i = 0; i < count; i++) {
this.deletestudent(student[i]);
}
int a = stmt.executeUpdate("DELETE FROM class where classno = '"
+ classno + "'");// 刪除學生
if (a != 0)
System.out.println("刪除班級" + classno + "成功!");
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deletestudent(int studentno) {// 刪除學生
try {
rs = stmt
.executeQuery("SELECT count(*) count FROM news where studentno = "
+ studentno);
rs.next();
int i = Integer.parseInt(rs.getString("count"));
while (i > 0) {
i--;
this.deletenews(studentno);// 刪除更新狀態
}
int a = stmt
.executeUpdate("DELETE FROM students where studentno = '"
+ studentno + "'");// 刪除學生
if (a != 0)
System.out.println("刪除學生" + studentno + "成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deletenews(int studentno) {// 刪除更新狀態
try {
int a = stmt.executeUpdate("DELETE FROM news where studentno = "
+ studentno);
if (a != 0)
System.out.println("刪除更新狀態" + studentno + "成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insertnews(int studentno, int accesscount, int credits,
int rank, int originalcount, int reprintcount, int translatecount,
int commentcount, boolean news) {// 插入更新狀態
try {
rs = stmt
.executeQuery("SELECT count(*) count FROM news where studentno = "
+ studentno);
rs.next();
int i = Integer.parseInt(rs.getString("count"));
while (i > 0) {
i--;
this.deletenews(studentno);// 刪除更新狀態
}
int a = stmt
.executeUpdate("insert into news(studentno,accesscount,credits,rank,originalcount,reprintcount,translatecount,commentcount,new) values("
+ studentno
+ ","
+ accesscount
+ ","
+ credits
+ ","
+ rank
+ ","
+ originalcount
+ ","
+ reprintcount
+ ","
+ translatecount
+ ","
+ commentcount + "," + news + ")");// 刪除學生
if (a != 0)
System.out.println("插入更新狀態" + studentno + "成功!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updatanews(int studentno, int accesscount, int credits,
int rank, int originalcount, int reprintcount, int translatecount,
int commentcount, boolean news) {// 更新更新狀態
try {
rs = stmt
.executeQuery("SELECT count(*) count FROM news where studentno = "
+ studentno);
rs.next();
int i = Integer.parseInt(rs.getString("count"));
if (i == 0) {
this.insertnews(studentno, accesscount, credits, rank,
originalcount, reprintcount, translatecount,
commentcount, news);// 刪除更新狀態
} else {
int a = stmt.executeUpdate("UPDATE news set studentno = "
+ studentno + ",accesscount = " + accesscount
+ ",credits = " + credits + ",rank = " + rank
+ ",originalcount = " + originalcount
+ ",reprintcount = " + reprintcount
+ ",translatecount = " + translatecount
+ ",commentcount = " + commentcount + ",new = " + news);//
if (a != 0)
System.out.println("更新狀態" + studentno + "成功!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeDB() throws SQLException {// 關閉數據庫連接
// rs.close();
stmt.close();
con.close();
}
/////////////////////////////////////////////系統生成get方法和set方法//////////////////////////////////////////////////////
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getDBname() {
return DBname;
}
public void setDBname(String dBname) {
DBname = dBname;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}