記錄java web專案讀取資料庫,寶塔建立資料庫過程
阿新 • • 發佈:2020-11-25
1、寶塔建立資料庫
在寶塔控制檯的軟體管理裡邊找到phpMyAdmin,點選安裝,並且埠號修改為888
2、在寶塔安全裡邊放開埠888和3306
3、雲伺服器安全組也同樣放開埠號
4、寶塔點選資料庫,新增資料庫,建立完成後,點選許可權,選擇所有人
5、資料庫建立完成,自行新增資料
6、資料庫配置完成後,在javaweb專案中訪問資料庫
定義資料庫屬性
public class StaticVar { //連結遠端伺服器資料庫 public static final String DB_URL = "jdbc:mysql://伺服器id:3306/user?useUnicode=true&characterEncoding=UTF-8"; public static final String USER_NAME = "資料庫名"; public static final String DB_PASSED = "資料庫密碼"; public static final String DRIVER_NAME = "com.mysql.jdbc.Driver"; }
管理類
package student.base; import java.sql.*; public class DBManager { private static DBManager instance; private Connection con; private ResultSet res; private Statement state; public static DBManager getInstance() { if (instance == null) { instance = newDBManager(); } return instance; } /** * 該方法使用者連線資料庫 * * @return 返回Connection的一個例項 */ private Connection getConnection() { try { Class.forName(StaticVar.DRIVER_NAME); con = DriverManager.getConnection(StaticVar.DB_URL, StaticVar.USER_NAME, StaticVar.DB_PASSED); }catch (ClassNotFoundException e) { return null; } catch (SQLException e) { return null; } return con; } /** * 用於查詢sql語句 * * @param sql sql語句 * @return 返回ResultSet集合 */ public ResultSet select(String sql) { con = getConnection(); if (!(con == null)) { try { state = con.createStatement(); res = state.executeQuery(sql); } catch (SQLException e) { return null; } } return res; } /** * 向表中插入一個元素,返回插入後的元素的id * * @param sql * @return */ public int insert(String sql) { int iId = -1; con = getConnection(); if (con != null) { try { state = con.createStatement(); int res = state.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); if (res != 0) { ResultSet rs = state.getGeneratedKeys(); if (rs.next()) { iId = rs.getInt(1); } } } catch (SQLException e) { iId = -1; } } if (state != null) { try { state.close(); } catch (SQLException e) { } } return iId; } /** * 修改表中的某個元素的數值 * * @param sql sql語句 * @return 元素是否被成功修改 */ public boolean update(String sql) { boolean updated = false; con = getConnection(); if (con != null) { try { state = con.createStatement(); int res = state.executeUpdate(sql); if (res == 0) { updated = false; } else { updated = true; } } catch (SQLException e) { updated = false; } } return updated; } /** * 刪除表中的某一個表項 * * @param sql sql語句 * @return 返回是否刪除成功 */ public boolean delete(String sql) { boolean deleted = false; con = getConnection(); if (con != null) { try { state = con.createStatement(); int res = state.executeUpdate(sql); if (res == 0) { deleted = false; } else { deleted = true; } } catch (SQLException e) { deleted = false; } } return deleted; } public void close() { try { if (res != null) { res.close(); } if (state != null) { state.close(); } if (con != null) { con.close(); con = null; } } catch (SQLException ignored) { } } }
連結資料庫,獲取資料,部分程式碼如下
if (check.getVersion() != null) { String selectSQL = "select * from story"; String title = null; String content = null; String url = null; list.clear(); try { DBManager dbManager = DBManager.getInstance(); ResultSet rs = dbManager.select(selectSQL); while (rs.next()) { title = rs.getString("title"); content = rs.getString("content"); url = rs.getString("url"); StoryInfo storyInfo = new StoryInfo(); storyInfo.setTitle(title); storyInfo.setContent(content); storyInfo.setUrl(url); list.add(storyInfo); } rs.close(); dbManager.close();
本地除錯,編譯,配置了Artifacts,就需要用buildArtifacts來編譯
資料獲取成功
7、生成的.war檔案上傳到伺服器tomcat對應的目錄下 tomcat\webapps
呼叫資料庫,本地呼叫正常,伺服器報錯 空指標異常
解決辦法:本地javaweb專案中lib目錄下
mysql-connector-java-5.1.45-bin.jar 和 spring-jdbc-5.2.3.RELEASE.jar複製到伺服器tomcat對應的lib目錄下,問題解決了
8、遇到tomcat輸出資訊亂碼,修改辦法如下:
tomcat\conf目錄下logging.properties 檔案
java.util.logging.ConsoleHandler.encoding = GBK
即可解決亂碼問題