Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I expect you have auto commit turned off for the connection.</p> <p>As far as I can understand your problem, this small code example will demonstrate it.</p> <pre><code>package org.yi.happy.mysql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class RepeatQuery { public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver").newInstance(); setupDatabase(); System.out.println("opening database..."); Connection connection = openConnection(); connection.setAutoCommit(false); queryDatabase(connection); deleteRecords(); queryDatabase(connection); connection.close(); } private static Connection openConnection() throws SQLException { return DriverManager.getConnection("jdbc:mysql://localhost/test", null, null); } private static void setupDatabase() throws SQLException { Connection conn = openConnection(); System.out.println("doing insert on another connection..."); PreparedStatement stmt = conn .prepareStatement("insert into strings(id, value) values(?, ?)"); stmt.setInt(1, 1); stmt.setString(2, "test"); stmt.execute(); stmt.setInt(1, 2); stmt.setString(2, "data"); stmt.execute(); stmt.close(); conn.close(); } private static void deleteRecords() throws SQLException { Connection conn = openConnection(); System.out.println("doing delete on another connection..."); PreparedStatement stmt = conn.prepareStatement("delete from strings"); stmt.execute(); stmt.close(); conn.close(); } private static void queryDatabase(Connection connection) throws SQLException { System.out.println("doing query..."); PreparedStatement ps = connection .prepareStatement("select id, value from strings"); ResultSet dbResult = ps.executeQuery(); while (dbResult.next()) { int id = dbResult.getInt(1); String value = dbResult.getString(2); System.out.println(id + " &lt;=&gt; " + value); } } } </code></pre> <p>The output of the above is</p> <pre><code>doing insert on another connection... opening database... doing query... 2 &lt;=&gt; data 1 &lt;=&gt; test doing delete on another connection... doing query... 2 &lt;=&gt; data 1 &lt;=&gt; test </code></pre> <p>If I change</p> <pre><code> connection.setAutoCommit(false); </code></pre> <p>to read</p> <pre><code> connection.setAutoCommit(true); </code></pre> <p>the output becomes</p> <pre><code>doing insert on another connection... opening database... doing query... 2 &lt;=&gt; data 1 &lt;=&gt; test doing delete on another connection... doing query... </code></pre> <p>which is what you appear to expect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload