Questions tagged [jdbc]

JDBC (Java DataBase Connectivity) is the base API which enables interacting with SQL database servers by executing SQL statements using the Java programming language.

JDBC is a Java-based data access technology (Java Standard Edition platform) from Oracle Corporation.

This technology is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the JVM host environment.

Online resources

Frequently asked questions

Related tag info pages

  • - another abstract layer over JDBC
  • - a part of data access layer with hidden low-level details privided by Spring
  • - JDBC-to-ODBC bridge
  • - JDBC driver for Oracle DB
  • - JDBC driver for MS SQL Server
  • - JDBC driver for PostgreSQL
  • - MySQL connectors including JDBC
  • - JDBC driver for Firebird
30949 questions
523
votes
7 answers

java.util.Date vs java.sql.Date

java.util.Date vs java.sql.Date: when to use which and why?
flybywire
  • 232,954
  • 184
  • 384
  • 491
431
votes
34 answers

MySQL JDBC Driver 5.1.33 - Time Zone Issue

Some background: I have a Java 1.6 webapp running on Tomcat 7. The database is MySQL 5.5. Previously, I was using Mysql JDBC driver 5.1.23 to connect to the DB. Everything worked. I recently upgraded to Mysql JDBC driver 5.1.33. After the upgrade,…
bluecollarcoder
  • 13,409
  • 4
  • 18
  • 18
408
votes
12 answers

How to get the insert ID in JDBC?

I want to INSERT a record in a database (which is Microsoft SQL Server in my case) using JDBC in Java. At the same time, I want to obtain the insert ID. How can I achieve this using JDBC API?
Satya
  • 7,542
  • 9
  • 35
  • 39
365
votes
30 answers

PreparedStatement IN clause alternatives?

What are the best workarounds for using a SQL IN clause with instances of java.sql.PreparedStatement, which is not supported for multiple values due to SQL injection attack security issues: One ? placeholder represents one value, rather than a list…
Chris Mazzola
  • 5,517
  • 5
  • 20
  • 15
335
votes
14 answers

Connect Java to a MySQL database

How do you connect to a MySQL database in Java? When I try, I get java.sql.SQLException: No suitable driver found for jdbc:mysql://database/table at java.sql.DriverManager.getConnection(DriverManager.java:689) at…
abson
  • 8,502
  • 15
  • 46
  • 67
333
votes
14 answers

To prevent a memory leak, the JDBC Driver has been forcibly unregistered

I am getting this message when I run my web application. It runs fine but I get this message during shutdown. SEVERE: A web application registered the JBDC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web…
mona
  • 5,501
  • 10
  • 35
  • 46
325
votes
21 answers

Java ResultSet how to check if there are any results

Resultset has no method for hasNext. I want to check if the resultSet has any value is this the correct way if (!resultSet.next() ) { System.out.println("no data"); }
kal
  • 26,197
  • 48
  • 119
  • 147
320
votes
16 answers

Connection pooling options with JDBC: DBCP vs C3P0

What is the best connection pooling library available for Java/JDBC? I'm considering the 2 main candidates (free / open-source): Apache DBCP - http://commons.apache.org/dbcp/ C3P0 - http://sourceforge.net/projects/c3p0 I've read a lot about them…
Dema
  • 6,697
  • 10
  • 38
  • 47
301
votes
21 answers

Find Oracle JDBC driver in Maven repository

I want to add the oracle jdbc driver to my project as dependency (runtime scope) - ojdbc14. In MVNrepository site the dependency to put in the POM is: com.oracle ojdbc14
rperez
  • 8,100
  • 10
  • 33
  • 44
297
votes
15 answers

How do I get the size of a java.sql.ResultSet?

Shouldn't this be a pretty straightforward operation? However, I see there's neither a size() nor length() method.
Jake
  • 14,329
  • 20
  • 64
  • 85
275
votes
12 answers

Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards?

It is said to be a good habit to close all JDBC resources after usage. But if I have the following code, is it necessary to close the Resultset and the Statement? Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { …
Zeemee
  • 9,655
  • 11
  • 48
  • 75
267
votes
8 answers

Java JDBC - How to connect to Oracle using Service Name instead of SID

I have a Java application that uses JDBC (via JPA) that was connecting to a development database using hostname, port and Oracle SID, like this: jdbc:oracle:thin:@oracle.hostserver1.mydomain.ca:1521:XYZ XYZ was the Oracle SID. Now I need to connect…
Jim Tough
  • 13,464
  • 23
  • 66
  • 90
267
votes
19 answers

"Incorrect string value" when trying to insert UTF-8 into MySQL via JDBC?

This is how my connection is set: Connection conn = DriverManager.getConnection(url + dbName + "?useUnicode=true&characterEncoding=utf-8", userName, password); And I'm getting the following error when tyring to add a row to a table: Incorrect string…
Lior
  • 4,766
  • 7
  • 27
  • 37
261
votes
41 answers

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

I'm working on getting my database to talk to my Java programs. Can someone give me a quick and dirty sample program using the JDBC? I'm getting a rather stupendous error: Exception in thread "main"…
Josh K
  • 26,152
  • 19
  • 79
  • 130
244
votes
14 answers

Retrieve column names from java.sql.ResultSet

With java.sql.ResultSet is there a way to get a column's name as a String by using the column's index? I had a look through the API doc but I can't find anything.
Ben
1
2 3
99 100