How do I get the row count in JDBC?

You’re going to have to do this as a separate query, for example: SELECT COUNT(1) FROM table_name Some JDBC drivers might tell you but this is optional behaviour and, more to the point, the driver may not know yet. This can be due to how the query is optimised eg two example execution strategies in … Read more

ResultSet not closed when connection closed?

One problem with ONLY closing the connection and not the result set, is that if your connection management code is using connection pooling, the connection.close() would just put the connection back in the pool. Additionally, some database have a cursor resource on the server that will not be freed properly unless it is explicitly closed.

com.mysql.jdbc.PacketTooBigException

I just had to deal with the same error message, thrown by DriverManager.getConnection(URL,username,password). I even got the same “magic numbers” (4739923 > 1048576); googling for those “magic numbers” will show results from people also getting the error thrown by some DriverManager.getConnection statement. In my case, the error message about packet size was COMPLETELY MISLEADING; I … Read more

How to pass Table-Valued parameters from java to sql server stored procedure?

With the inputs provided by Mark Rotteveel I was able to do it. Thanks Mark, Sean thanks for your input as well. Here is the working code for any of you that may find it useful. String jdbcurl = “jdbc:sqlserver://TestServer:1433;DatabaseName=Student”; connection = DriverManager.getConnection(jdbcurl,”username”,”password”); SQLServerDataTable stuTypeDT = new SQLServerDataTable(); stuTypeDT.addColumnMetadata(“StudentId”, java.sql.Types.NUMERIC); stuTypeDT.addColumnMetadata(“Name”, java.sql.Types.VARCHAR); stuTypeDT.addColumnMetadata(“Department”, java.sql.Types.VARCHAR); stuTypeDT.addColumnMetadata(“Address”, … Read more

Call stored procedure with table-valued parameter from java

This is documented here in the JDBC driver manual. In your case, you’d have to do this: try (SQLServerCallableStatement stmt = (SQLServerCallableStatement) con.prepareCall(“{call test(?)}”)) { SQLServerDataTable table = new SQLServerDataTable(); sourceDataTable.addColumnMetadata(“n”, java.sql.Types.INTEGER); sourceDataTable.addRow(9); sourceDataTable.addRow(12); sourceDataTable.addRow(27); sourceDataTable.addRow(37); stmt.setStructured(1, “dbo.integer_list_tbltype”, table); } I’ve also recently documented this in an article.