Decorator for JDBC PreparedStatement, prints queries in readable format. Internal implementation of PreparedStatement is unchanged.
Copy the file into your project (jar and/or maven dependency may be coming in the future)
String query = "INSERT INTO table_name VALUES (?, ?, ?, ?)"
PrintablePreparedStatement ps =
new PrintablePreparedStatement(connection.prepareStatement(query), query);
Where connection is an instance of java.sql.Connection
- Any query can be passed as a parameter
- With the default settings, calling
ps.execute(),ps.executeQuery(), orps.executeUpdate()will print the query before executing it
String query = "INSERT INTO table_name VALUES (?, ?, ?, ?)"
PrintablePreparedStatement ps =
new PrintablePreparedStatement(connection.prepareStatement(query), query, false);
PrintablePreparedStatement.autoPrint = false; // true by default
System.out.println(ps.toString());
or
ps.print();
- Code used
- Output
setBoolean()
setShort()
setInt()
setString()
setLong()
setFloat()
setDouble()
setBigDecimal()
setDate()
setTime()
setTimestamp()
setObject() // the object's toString() method will be called.
- May not work if the table name or the attribute contains a '?'
- Does not work with arrays or streams
- Feel free to make an issue if a particular setter isn't printing properly, or if you'd like one that isn't listed above

