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
33118 questions
556
votes
7 answers

java.util.Date vs java.sql.Date

java.util.Date vs java.sql.Date: when to use which and why?
flybywire
  • 261,858
  • 191
  • 397
  • 503
467
votes
35 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
  • 14,339
  • 4
  • 22
  • 18
430
votes
15 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
  • 8,146
  • 9
  • 38
  • 43
389
votes
33 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,807
  • 5
  • 22
  • 15
362
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
  • 9,148
  • 17
  • 50
  • 69
353
votes
23 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
  • 28,545
  • 49
  • 129
  • 149
351
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
  • 6,079
  • 12
  • 41
  • 46
326
votes
15 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,867
  • 11
  • 40
  • 48
317
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
  • 15,007
  • 22
  • 70
  • 86
317
votes
23 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
  • 5,454
  • 8
  • 30
  • 38
316
votes
51 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
  • 28,364
  • 20
  • 86
  • 132
308
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,430
  • 11
  • 36
  • 44
298
votes
11 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
  • 10,486
  • 14
  • 51
  • 81
295
votes
8 answers

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
  • 14,843
  • 23
  • 75
  • 96
276
votes
25 answers

Solving a "communications link failure" with JDBC and MySQL

I'm trying to connect to the local MySQL server but I keep getting an error. Here is the code. public class Connect { public static void main(String[] args) { Connection conn = null; try { String userName =…
Anthony
  • 2,813
  • 3
  • 16
  • 9
1
2 3
99 100