gpt4 book ai didi

java - MySQL 更改表标题

转载 作者:行者123 更新时间:2023-11-29 03:30:36 24 4
gpt4 key购买 nike

当我使用 PreparedStatment 向数据库发出请求时,mysql 添加“测试”。表标题的前缀。比如……我的数据库请求方式:

    public static ResultSet databaseRequest(String request, String ... args){
try{
if(connection != null){
PreparedStatement prepState = connection.prepareStatement(request);
for(int index = 0; index < args.length; index++){
prepState.setString(index + 1, args[index]);
}
ResultSet result = prepState.executeQuery();
return result;
} else {
throw new SQLException("Database connection had't been initialized");
}
} catch(SQLException exception){
System.out.println("Error while requesting database: " + exception.getSQLState());
exception.printStackTrace();
}
return null;
}

我如何使用它:

ResultSet result = DatabaseUtils.databaseRequest("SELECT * FROM `table`");

错误 - http://pastebin.com/Jxpi70Ea

如何避免?

最佳答案

在连接 url 中,您应该指定用作默认数据库的初始数据库。当指定默认数据库时,您可以使用不带数据库前缀的表名。

Initial Database for Connection

If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.

Note

Always use the Connection.setCatalog() method to specify the desired database in JDBC applications, rather than the USE database statement.

关于java - MySQL 更改表标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31223708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com