Contents
PrintablePreparedStatement
Decorator for JDBC PreparedStatement, prints queries in readable format. Internal implementation of PreparedStatement is unchanged.
Installation
Copy the file into your project (jar and/or maven dependency may be coming in the future)
Usage
To instantiate
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
To instantiate with auto-printing turned off
String query = "INSERT INTO table_name VALUES (?, ?, ?, ?)"
PrintablePreparedStatement ps =
new PrintablePreparedStatement(connection.prepareStatement(query), query, false);
To toggle auto-printing (application-wise)
PrintablePreparedStatement.autoPrint = false; // true by default
To print manually
System.out.println(ps.toString());
or
ps.print();
Sample Output
- Code used
- Output
Current working setters
setBoolean()
setShort()
setInt()
setString()
setLong()
setFloat()
setDouble()
setBigDecimal()
setDate()
setTime()
setTimestamp()
setObject() // the object's toString() method will be called.
Known issues
- May not work if the table name or the attribute contains a '?'
- Does not work with arrays or streams
Missing features?
- 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