Skip to content
Advertisement

Tag: jdbc

what databases can be used with java?

I am doing an undergrad final project, and need to justify my choice of MySQL for the database element of my project. Truth is, it’s the only one I can really use, and hence I went for it. What other database systems could I have used? Any advantages and disadvantages of these over MySQL? Answer In fact, you can use

How to quote/escape identifiers such as column names with JDBC?

Different database servers use different ways to quote and escape identifiers. E.g. “foo bar” vs `foo bar` vs [foo bar], or “10””” vs “10””, or identifiers such as FooBar or array need to be quoted for some databases but not for others. Is there any API method that performs the quoting/escaping correctly for a given database connection? Or any alternative

How to catch a specific exception in JDBC?

How to catch specific exceptions in JDBC? Examples: primary key exception or foreign key exception. Answer SQLException contains some database-specific info related to the exception. From the doc: Each SQLException provides several kinds of information: 1) a string describing the error. This is used as the Java Exception message, available via the method getMesage. 2) a “SQLstate” string, which follows

Cannot issue data manipulation statements with executeQuery()

In MySQL I have two tables, tableA and tableB. I am trying to execute two queries: But I get the following error: What does this mean? Answer To manipulate data you actually need executeUpdate() rather than executeQuery(). Here’s an extract from the executeUpdate() javadoc which is already an answer at its own: Executes the given SQL statement, which may be

What are all the possible values for SQLException.getSQLState?

SQLException.getSQLState retrieves the SQLState for the SQLException object. What are all the possible values that can be returned by this method? Can I use the value to identify specific errors that occurred in the database (i.e. can this value tell me if it was a PK violation, or a unique constraint, or column value too large, etc.)? Also, the DatabaseMetaData.getSQLStateType()

PreparedStatement setNull(..)

Java PreparedStatement provides a possibility to explicitely set a Null value. This possibility is: Are the semantics of this call the same as when using a specific setType with a null parameter? ? Answer This guide says: 6.1.5 Sending JDBC NULL as an IN parameter The setNull method allows a programmer to send a JDBC NULL (a generic SQL NULL)

How do I manually configure a DataSource in Java?

I’m trying to follow Sun’s JDBC tutorial at http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html It gives the following example code: This code doesn’t compile because the DataSource interface has none of these methods, except for the getConnection() method invoked last. (Here’s the javadoc: http://java.sun.com/javase/6/docs/api/javax/sql/DataSource.html) What am I missing? Edit: I’m actually trying to connect to MySQL (com.mysql.jdbc) and I can’t find the javadoc for that.

Advertisement