简体   繁体   English

mysql查询不在java中执行,但在我的SQLYog中执行

[英]mysql query not executing in java but executing in my SQLYog

can anyone please tell why the following update query which is working perfectly when executed directly from my SQLYog editor, but not executing from java. 任何人都可以告诉我为什么下面的更新查询在直接从我的SQLYog编辑器直接执行而不从Java执行时却能正常工作。 it is not giving any exception but not updating into the database. 它没有给出任何异常,但没有更新到数据库中。

this the update query 这是更新查询

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 code 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;
}

This is how I used to do it before I switched to Spring's JdbcTemplate framework. 在切换到Spring的JdbcTemplate框架之前,这就是我以前这样做的方式。 Maybe this will help. 也许这会有所帮助。 It looks very similar to yours. 它看起来与您的非常相似。

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;
}

There are some subtle differences. 有一些细微的差异。 I do a commit, though autoCommit is the default. 我进行了一次提交,尽管默认是autoCommit。

像这样尝试:DriverManager.getConnection(“ jdbc:mysql:// localhost:3306 / kot”,“ root”,“ root”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM