JDBCUtils无法找到配置文件路径
把db.properties
文件与JDBCUtils
类放在同一包内,
使用以下方式均无法寻找到相对路径:
InputStream in = new FileInputStream("db.properties");
InputStream in = new FileInputStream("src/com/web/utils/db.properties");
InputStream in = JDBCUtils.class.getClassLoader.getResourceAsStream("db.properties");
而修改为以下方式,可获得正确路径:
InputStream in = new FileInputStream(JDBCUtils.class.getResource("db.properties").getPath());
prop.load(in);
in.close();
原理暂未研究。
报错:org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
说明问题来源可能主要有四个:
- 数据库名称错误(检查properties配置文件的url中的数据库名与本地数据库名是否一致)
- 用户名或密码错误(会有更明显的提示,检查是否与本地不一致)
- 主机IP地址或端口号错误(将localhost修改为127.0.0.1,端口号通常默认使用3306)
- 如果报错信息很长,如下:
The server time zone value ‘�й���ʱ��’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
说明无法解析时区,只需在url后加入?serverTimezone=UTC
即可。
相对路径问题(包括在JSP文件中)
场景 | 写法 |
---|---|
JSP引用同一目录下的外部文件 | “file.type” |
JSP引用同一目录下的文件夹中的外部文件 | “dir/file.type” |
其他情况下引用外部文件 | 在工程中的路径,注意包名中的“.”需替换成“/” |
后台Servlet转移到JSP文件 | “/file.jsp” |
其他情况下引用JSP文件 | “projectname/file.jsp” |
Mysql语句
mysql语句中保留字是大小写不敏感的。
mysql语句需注意分号的使用。
作用 | 写法 |
---|---|
创建库 | create database `users`; |
删除库 | drop database `users`; |
创建表 | create tab |