gpt4 book ai didi

java - mysql 查询不是在 java 中执行而是在我的 SQLYog 中执行

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

谁能告诉我为什么下面的更新查询在直接从我的 SQLYog 编辑器中执行时运行良好,而不是从 java 中执行。它没有给出任何异常,但没有更新到数据库中。

这是更新查询

UPDATE hotel_tables SET hotel_tables.status='reserved' WHERE hotel_tables.section='pub' AND tableno='4' AND ('4' NOT IN (SELECT tableno FROM table_orders WHERE outlet='pub'))

Java代码

public static void main(String[] args) throws Exception {
int update = new Dbhandler().update("UPDATE hotel_tables SET hotel_tables.status='reserved' WHERE hotel_tables.section='pub' AND tableno='4' AND ('4' NOT IN (SELECT tableno FROM table_orders WHERE outlet='pub'))");
}

public int update(String Query)throws Exception
{
try
{
cn=getconn();
stmt=(Statement) cn.createStatement();
n=stmt.executeUpdate(Query);
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
throw(e);
}
finally
{
cn.close();
}
return n;
}

public Connection getconn()
{
try
{
Class.forName(driver).newInstance();
String url="jdbc:mysql://localhost/kot?user=root&password=root";
cn=(Connection) DriverManager.getConnection(url);
}
catch(Exception e)
{
System.out.println("DBHandler ERROR:"+e);
}
return cn;
}

最佳答案

这就是我在切换到 Spring 的 JdbcTemplate 框架之前的做法。也许这会有所帮助。它看起来与您的非常相似。

public static int runUpdate(String query, DataSource ds, Object... params) throws SQLException  {
int rowsAffected = 0;
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = ds.getConnection();
stmt = conn.prepareStatement(query);
int paramCount = 1;
for (Object param : params) {
stmt.setObject(paramCount++, param);
}
rowsAffected = stmt.executeUpdate();
conn.commit();
} catch (SQLException sqle) {
throw sqle;
//log error
} finally {
closeConnections(conn, stmt, null);
}
return rowsAffected;
}

有一些细微的差别。我做了一个提交,尽管 autoCommit 是默认设置。

关于java - mysql 查询不是在 java 中执行而是在我的 SQLYog 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19658155/

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