这个类是这个课程设计中的一个类,非独立运行(但是测试连接数据库可以独立运行,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; } // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// }