我之前用 MySQL 运行我的 java 程序,一切都很好。现在我将程序连接到 PostgreSQL,但它给了我一些错误,我不知道我应该在哪里找到问题以及如何解决它。如果有人可以提供帮助,我将不胜感激。
我在用:
- 爪哇 15
- 弹簧靴
- 雄猫 9
- PostgreSQL 13
- PostgreSQL JDBC 42.2.23
为了运行 SQL 查询和创建数据库表,我使用了一个映射器存储库,它有一个带接口的抽象类,我为每个类定制了映射器。
这是我用于连接数据库的连接池:
import org.apache.commons.dbcp.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectionPool {
private static BasicDataSource ds = new BasicDataSource();
private final static String dbURL = "jdbc:postgresql://...:.../SomeThing?
characterEncoding=utf8";
private final static String dbUserName = "...";
private ConnectionPool() {
}
static {
ds.setDriverClassName("org.postgresql.Driver");
// remote db
ds.setUrl(dbURL);
ds.setUsername(dbUserName);
ds.setPassword(dbPassword);
ds.setMinIdle(1);
ds.setMaxIdle(2000);
ds.setMaxOpenPreparedStatements(2000);
setEncoding();
}
public static Connection getConnection() throws SQLException {
try {
return ds.getConnection();
}catch (Exception e){
ds.setPassword(dbPassword);
return ds.getConnection();
}
}
public static void setEncoding(){
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
statement.execute("ALTER DATABASE ... CHARACTER SET utf8mb4 COLLATE
utf8mb4_unicode_ci;");
connection.close();
statement.close();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
}
这是我的课程映射器类的示例:
public class CourseMapper extends Mapper<Offering, CustomPair> implements ICourseMapper {
private static final String COLUMNS = " code, classCode, name, units, type, instructor, capacity, signedUp, start, end, time ";
private static final String TABLE_NAME = "COURSES";
public CourseMapper(Boolean doManage) throws SQLException {
if (doManage) {
Connection con = ConnectionPool.getConnection();
Statement st = con.createStatement();
st.executeUpdate(String.format("DROP TABLE IF EXISTS %s", TABLE_NAME));
st.executeUpdate(String.format(
"create table %s (\n" +
" code varchar(255) not null,\n" +
" classCode varchar(255) not null,\n" +
" name tinytext not null,\n" +
" units int not null,\n" +
" type tinytext not null,\n" +
" instructor tinytext not null,\n" +
" capacity int not null,\n" +
" signedUp int not null default 0,\n" +
" start tinytext not null,\n" +
" end tinytext not null,\n" +
" time tinytext not null,\n" +
" primary key(code, classCode)\n" +
");",
TABLE_NAME));
st.close();
con.close();
}
}
public CourseMapper() throws SQLException {
}
@Override
protected String getFindStatement(CustomPair id) {
return String.format("select * from %s where %s.%s = %s and %s.%s = %s", TABLE_NAME, TABLE_NAME, "code",
StringUtils.quoteWrapper(id.getArgs().get(0)), TABLE_NAME, "classCode", StringUtils.quoteWrapper(id.getArgs().get(1)));
}
有了这个,我能够在 MySQL 数据库中创建表并实现查询(查找、插入、删除...)
但是,现在我想使用 PostgreSQL 数据库,通过运行项目它给了我这些错误:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "end"
Position: 304
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284)
at org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:258)
at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at com.internet.engineering.IECA5.repository.Course.CourseMapper.<init>(CourseMapper.java:24)
at com.internet.engineering.IECA5.repository.MzRepository.createAllTables(MzRepository.java:35)
at com.internet.engineering.IECA5.IeCa5Application.main(IeCa5Application.java:18)
org.postgresql.util.PSQLException: ERROR: type "tinytext" does not exist
Position: 67
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:401)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:322)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:308)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284)
at org.postgresql.jdbc.PgStatement.executeUpdate(PgStatement.java:258)
at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at org.apache.commons.dbcp.DelegatingStatement.executeUpdate(DelegatingStatement.java:228)
at com.internet.engineering.IECA5.repository.Student.StudentMapper.<init>(StudentMapper.java:22)