Java面向物件與多執行緒綜合實驗(五)之JDBC
阿新 • • 發佈:2018-12-11
本次程式碼沿用第三次中的User.java Administrator.java Operator.java Browser.java和Doc.java
以及第四次中的LoginWindow.java MenuWindow.java UserWindow.java UpDownloadWindow.java和PasswordWindow.java
實驗目的
理解 JDBC的特點,結構,應用模型;瞭解JDBC驅動程式的型別;掌握通過JDBC訪問資料庫執行SQL 語句的方法。
實驗內容
編寫程式,將前面課程所編寫的檔案管理系統的資料管理方式改寫為資料庫儲存。(建議將所有資料庫操作封裝在DataProcessing類中,並保留原來介面,這樣可保持原有程式不做大的變化)
按照視訊中的操作按照好MySQL和MySQL—Front之後修改如下程式碼
需要執行的主程式
import java.sql.SQLException; public class WinMain { public static void main(String args[]) { String driverName="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/document"; String user="root"; String password="123456"; try { DataProcessing.connectToDatabase(driverName, url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } LoginWindow window=new LoginWindow(); window.loginFrame(); } }
資料庫執行函式
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.util.*; public class DataProcessing{ private static Connection connection; private static Statement statement; @SuppressWarnings("unused") private static PreparedStatement preparedStatement; private static ResultSet resultSet; private static boolean connectedToDatabase=false; public static void connectToDatabase(String driveName,String url,String user,String password) throws ClassNotFoundException, SQLException { Class.forName(driveName); connection=DriverManager.getConnection(url, user, password); connectedToDatabase=true; } public static void disconnectFromDatabase() { if(connectedToDatabase) { try { resultSet.close(); statement.close(); connection.close(); }catch(SQLException sqlException) { sqlException.printStackTrace(); }finally { connectedToDatabase=false; } } } public static Doc searchDoc(String DocID) throws SQLException{ Doc temp=null; if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); String sql="select * from doc_info where Id='"+DocID+"'"; resultSet=statement.executeQuery(sql); if(resultSet.next()) { String ID=resultSet.getString("ID"); String creator=resultSet.getString("creator"); Timestamp timestamp=resultSet.getTimestamp("timestamp"); String description=resultSet.getString("description"); String filename=resultSet.getString("filename"); temp=new Doc(ID,creator,timestamp,description,filename); } return temp; } public static Enumeration<Doc> getAllDocs() throws SQLException{ Hashtable<String,Doc> docs=new Hashtable<String,Doc>(); Doc temp=null; Enumeration<Doc> e; if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); String sql="select * from doc_info"; resultSet=statement.executeQuery(sql); while(resultSet.next()) { String ID=resultSet.getString("ID"); String creator=resultSet.getString("creator"); Timestamp timestamp=resultSet.getTimestamp("timestamp"); String description=resultSet.getString("description"); String filename=resultSet.getString("filename"); temp=new Doc(ID,creator,timestamp,description,filename); docs.put(ID, temp); } e=docs.elements(); return e; } public static boolean insertDoc(String ID,String creator,Timestamp timestamp,String description,String filename)throws SQLException{ if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from doc_info where Id='"+ID+"'"; resultSet=statement.executeQuery(sql); if(resultSet.next()) return false; sql="insert into doc_info(Id,creator,timestamp,description,filename) values "+"('"+ID+"','"+creator+"','"+timestamp+"','"+description+"','"+filename+"')"; if(statement.executeUpdate(sql)>0) return true; else return false; } public static User searchUser(String name) throws SQLException{ User temp=null; if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); String sql="select * from user_info where username ='"+name+"'"; resultSet=statement.executeQuery(sql); if(resultSet.next()) { String Name=resultSet.getString("username"); String password=resultSet.getString("password"); String role=resultSet.getString("role"); temp=new User(Name,password,role); } return temp; } public static User search(String name,String password) throws SQLException{ User temp=null; if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); String sql="select * from user_info where username='"+name+"'and password='"+password+"'"; resultSet=statement.executeQuery(sql); if(resultSet.next()) { String Name=resultSet.getString("username"); String Password=resultSet.getString("password"); String role=resultSet.getString("role"); temp=new User(Name,Password,role); } return temp; } public static Enumeration<User> getAllUser() throws SQLException{ Hashtable<String,User> users=new Hashtable<String,User>(); User temp = null; Enumeration<User> e; if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); String sql="select * from user_info"; resultSet=statement.executeQuery(sql); while(resultSet.next()) { String name=resultSet.getString("username"); String password=resultSet.getString("password"); String role=resultSet.getString("role"); temp=new User(name,password,role); users.put(name, temp); } e=users.elements(); return e; } public static boolean update(String name,String password,String role) throws SQLException{ if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from user_info where username='"+name+"'"; resultSet=statement.executeQuery(sql); if(!resultSet.next()) return false; sql="update user_info set password='"+password+"',role='"+role+"' where username='"+name+"'"; if(statement.executeUpdate(sql)>0) return true; else return false; } public static boolean insert(String name,String password,String role) throws SQLException{ if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from user_info where username='"+name+"'"; resultSet=statement.executeQuery(sql); if(resultSet.next()) return false; sql="insert into user_info(username,password,role) values "+"('"+name+"','"+password+"','"+role+"')"; if(statement.executeUpdate(sql)>0) return true; else return false; } public static boolean delete(String name) throws SQLException{ if(!connectedToDatabase) throw new SQLException("Not Connected to Database."); statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from user_info where username='"+name+"'"; resultSet=statement.executeQuery(sql); if(!resultSet.next()) return false; sql="delete from user_info where username='"+name+"'"; if(statement.executeUpdate(sql)>0) return true; else return false; } }
以下是需要完成的結果展示(附帶MySQL資料庫安裝教程)Java面向物件與多執行緒綜合實驗(五)之JDBC