记录java web项目读取数据库,宝塔创建数据库过程
1、宝塔创建数据库
在宝塔控制台的软件管理里边找到phpMyAdmin,点击安装,并且端口号修改为888
2、在宝塔安全里边放开端口888 和3306
3、云服务器安全组也同样放开端口号
4、宝塔点击数据库,添加数据库,创建完成后,点击权限,选择所有人
5、数据库创建完成,自行添加数据
6、数据库配置完成后,在java web项目中访问数据库
定义数据库属性
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 = new DBManager(); } 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,就需要用build Artifacts来编译
数据获取成功
7、生成的.war文件上传到服务器tomcat对应的目录下 tomcat\webapps
调用数据库,本地调用正常,服务器报错 空指针异常
解决办法:本地java web项目中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
即可解决乱码问题