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
264
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
248
votes
15 answers

Difference between Statement and PreparedStatement

The Prepared Statement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement. The Prepared Statement may be parametrized Most relational databases handles a JDBC / SQL query in…
CodeBee..
  • 2,606
  • 3
  • 17
  • 7
223
votes
9 answers

Is it possible to specify the schema when connecting to postgres with JDBC?

Is it possible? Can i specify it on the connection URL? How to do that?
marcosbeirigo
  • 11,098
  • 6
  • 39
  • 57
216
votes
11 answers

Simulate CREATE DATABASE IF NOT EXISTS for PostgreSQL?

I want to create a database which does not exist through JDBC. Unlike MySQL, PostgreSQL does not support create if not exists syntax. What is the best way to accomplish this? The application does not know if the database exists or not. It should…
Aman Deep Gautam
  • 8,091
  • 21
  • 74
  • 130
214
votes
33 answers

ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

I have installed Oracle 11g Express Edition Release 2 in my windows 7 64 bit OS and tried to execute JDBC program, then I got the following error: java.sql.SQLException: Listener refused the connection with the following error: ORA-12505,…
Learner
  • 2,141
  • 2
  • 13
  • 5
211
votes
6 answers

Using "like" wildcard in prepared statement

I am using prepared statements to execute mysql database queries. And I want to implement a search functionality based on a keyword of sorts. For that I need to use LIKE keyword, that much I know. And I have also used prepared statements before,…
ssn
  • 2,631
  • 4
  • 24
  • 28
193
votes
5 answers

How to execute IN() SQL queries with Spring's JDBCTemplate effectively?

I was wondering if there is a more elegant way to do IN() queries with Spring's JDBCTemplate. Currently I do something like that: StringBuilder jobTypeInClauseBuilder = new StringBuilder(); for(int i = 0; i < jobTypes.length; i++) { Type jobType…
Malax
  • 9,436
  • 9
  • 48
  • 64
192
votes
8 answers

Get query from java.sql.PreparedStatement

In my code I am using java.sql.PreparedStatement. I then execute the setString() method to populate the wildcards of the prepared statement. Is there a way for me to retrieve (and print out) the final query before the executeQuery() method is called…
llm
  • 5,539
  • 12
  • 36
  • 30
191
votes
15 answers

How can I get the SQL of a PreparedStatement?

I have a general Java method with the following method signature: private static ResultSet runSQLResultSet(String sql, Object... queryParams) It opens a connection, builds a PreparedStatement using the sql statement and the parameters in the…
froadie
  • 79,995
  • 75
  • 166
  • 235
176
votes
16 answers

Is asynchronous jdbc call possible?

I wonder if there is a way to make asynchronous calls to a database? For instance, imagine that I've a big request that take a very long time to process, I want to send the request and receive a notification when the request will return a value (by…
Steve Gury
  • 15,158
  • 6
  • 38
  • 42
172
votes
12 answers

How do I find out my MySQL URL, host, port and username?

I need to find my MySQL username. When I open the MySQL command line client, it only asks me for my password. I don't remember my username. And for connectivity with JDBC, I need the URL, host and port number. Where do I find all of these?
shilps
166
votes
7 answers

org.postgresql.util.PSQLException: FATAL: sorry, too many clients already

I am trying to connect to a Postgresql database, I am getting the following Error: Error:org.postgresql.util.PSQLException: FATAL: sorry, too many clients already What does the error mean and how do I fix it? My server.properties file is…
lakshmi
  • 4,539
  • 17
  • 47
  • 55
165
votes
5 answers

How should I use try-with-resources with JDBC?

I have a method for getting users from a database with JDBC: public List getUser(int userId) { String sql = "SELECT id, name FROM users WHERE id = ?"; List users = new ArrayList(); try { Connection con =…
Jonas
  • 121,568
  • 97
  • 310
  • 388
165
votes
7 answers

JPA or JDBC, how are they different?

I am learning Java EE and I downloaded the eclipse with glassfish for the same. I saw some examples and also read the Oracle docs to know all about Java EE 5. Connecting to a database was very simple. I opened a dynamic web project, created a…
user907810
  • 3,208
  • 10
  • 39
  • 47
158
votes
12 answers

'0000-00-00 00:00:00' can not be represented as java.sql.Timestamp error

I have a database table containing dates (`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'). I'm using MySQL. From the program sometimes data is passed without the date to the database. So, the date value is auto assigned to 0000-00-00…
Chamila Adhikarinayake
  • 3,588
  • 5
  • 25
  • 32