Skip to content

Latest commit

 

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

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(), or ps.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

Screen Shot 2021-04-15 at 12 34 22 AM

  • Output

Screen Shot 2021-04-15 at 12 28 45 AM

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

About

Decorator for java.sql.PreparedStatement, prints queries in readable format

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages