You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Java JDBC Connector插入数据无报错但未插入行问题求助

Troubleshooting JDBC Insert: No Errors, But No Rows Added

Hey there, let's break down this frustrating issue—when your JDBC code runs without errors but doesn't add rows to the database, the culprit is almost always related to transaction handling or missing execution steps. Let's go through the most likely fixes and checks:

1. You Forgot to Commit the Transaction (The #1 Culprit!)

Looking at your constructor, you explicitly set connection.setAutoCommit(false);—this disables JDBC's automatic commit behavior for transactions. That means after running your insert statement, you must manually call connection.commit(); to save the changes to the database. If you skip this, the transaction will be rolled back automatically when the connection closes, leaving no trace of your insert.

Fix Example:

Add the commit call right after executing your insert in the crearProyecto method:

public void crearProyecto(String proyectoNombre) {
    sentence = "INSERT INTO tu_tabla (nombre_columna) VALUES ('" + proyectoNombre + "')";
    try {
        // Execute the insert and check how many rows were affected
        int rowsAffected = st.executeUpdate(sentence);
        System.out.println("Rows affected: " + rowsAffected); // This tells you if the SQL ran
        
        // Critical: Commit the transaction
        connection.commit();
    } catch (SQLException e) {
        e.printStackTrace();
        // Always rollback on failure to avoid hanging transactions
        try {
            connection.rollback();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

2. Verify Your SQL Execution Logic

  • Are you using executeUpdate() instead of executeQuery()? For insert/update/delete operations, you need executeUpdate()—it returns the number of rows affected, which is a great way to confirm if your SQL actually ran (a return value of 0 means no rows were touched, which points to a SQL issue).
  • Double-check that your sentence variable contains a valid SQL statement. Try running the exact SQL string directly in your MySQL client—if it doesn't insert rows there, the problem is with your SQL syntax (e.g., missing required fields, incorrect table/column names, or unescaped string values).

3. Catch All Exceptions in crearProyecto

Your constructor has try-catch blocks, but if crearProyecto doesn't handle SQLException, errors could be silently swallowed (for example, if the SQL is invalid but the exception isn't printed). Always wrap your SQL execution code in a try-catch to log full error details—this will reveal issues you might not be seeing right now.

4. Bonus: Avoid Statement to Prevent SQL Injection & Syntax Errors

Using Statement is risky (it's vulnerable to SQL injection) and prone to syntax mistakes (like forgetting quotes around string values). Switch to PreparedStatement instead—it's safer, cleaner, and handles value escaping automatically:

public void crearProyecto(String proyectoNombre) {
    String sql = "INSERT INTO tu_tabla (nombre_columna) VALUES (?)";
    try (PreparedStatement pst = connection.prepareStatement(sql)) {
        pst.setString(1, proyectoNombre); // Bind the value safely
        int rowsAffected = pst.executeUpdate();
        System.out.println("Rows affected: " + rowsAffected);
        connection.commit();
    } catch (SQLException e) {
        e.printStackTrace();
        try {
            connection.rollback();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

5. Check for Accidental Rollbacks or Closed Connections

  • Make sure nowhere in your code are you calling connection.rollback(); before committing.
  • Ensure the connection isn't being closed before you commit the transaction—closing a connection with an uncommitted transaction will trigger an automatic rollback.

Start with checking the transaction commit first—this is by far the most common reason for this exact issue. If that doesn't fix it, move on to verifying your SQL and exception handling.

内容的提问来源于stack exchange,提问作者Fernando

火山引擎 最新活动