簡體   English   中英

mysql查詢不在java中執行,但在我的SQLYog中執行

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

任何人都可以告訴我為什么下面的更新查詢在直接從我的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。

像這樣嘗試:DriverManager.getConnection(“ jdbc:mysql:// localhost:3306 / kot”,“ root”,“ root”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM