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.
33,430
questions
560
votes
7
answers
311k
views
java.util.Date vs java.sql.Date
java.util.Date vs java.sql.Date: when to use which and why?
470
votes
35
answers
755k
views
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 ...
440
votes
16
answers
374k
views
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?
395
votes
33
answers
386k
views
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 ? ...
372
votes
14
answers
790k
views
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(...
354
votes
23
answers
557k
views
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");
}
354
votes
14
answers
445k
views
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] ...
330
votes
25
answers
577k
views
"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 ...
326
votes
51
answers
1.0m
views
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"...
326
votes
15
answers
216k
views
Connection pooling options with JDBC: DBCP vs C3P0 [closed]
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://...
321
votes
16
answers
573k
views
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.
308
votes
22
answers
482k
views
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:
<dependency>
<groupId>com.oracle&...
302
votes
11
answers
249k
views
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;
...
296
votes
8
answers
800k
views
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:...
291
votes
25
answers
1.4m
views
Solving a "communications link failure" with JDBC and MySQL [duplicate]
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;
...
270
votes
14
answers
439k
views
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.
251
votes
15
answers
291k
views
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 ...
237
votes
12
answers
301k
views
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 ...
236
votes
10
answers
332k
views
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?
222
votes
33
answers
1.1m
views
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 ...
214
votes
6
answers
232k
views
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. ...
195
votes
5
answers
226k
views
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 ...
195
votes
15
answers
332k
views
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 ...
195
votes
8
answers
429k
views
Get query from java.sql.PreparedStatement [duplicate]
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 ...
190
votes
7
answers
430k
views
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 ...
177
votes
16
answers
94k
views
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 ...
175
votes
12
answers
642k
views
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 ...
170
votes
7
answers
173k
views
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. ...
166
votes
5
answers
188k
views
How should I use try-with-resources with JDBC?
I have a method for getting users from a database with JDBC:
public List<User> getUser(int userId) {
String sql = "SELECT id, name FROM users WHERE id = ?";
List<User> users = new ...
158
votes
10
answers
136k
views
How does a PreparedStatement avoid or prevent SQL injection?
I know that PreparedStatements avoid/prevent SQL Injection. How does it do that? Will the final form query that is constructed using PreparedStatements be a string or otherwise?
158
votes
12
answers
139k
views
'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,...
154
votes
14
answers
401k
views
PreparedStatement with list of parameters in a IN clause [duplicate]
How to set value for in clause in a preparedStatement in JDBC while executing a query.
Example:
connection.prepareStatement("Select * from test where field in (?)");
If this in-clause can hold ...
153
votes
8
answers
62k
views
Caused by: java.lang.Exception: No native library is found for os.name=Mac and os.arch=aarch64. path=/org/sqlite/native/Mac/aarch64
I am using Android Studio [Android Studio Arctic Fox | 2020.3.1 Patch 1]
My room library version is [2.3.0]
Used Gradle version [7.0.1]
Also added kapt 'org.xerial:sqlite-jdbc:3.36.0.1'
Caused by: ...
139
votes
7
answers
229k
views
MySQL 'create schema' and 'create database' - Is there any difference
Taking a peek into the information_schema database and peeking at the metadata for one of my pet projects, I'm having a hard time understanding what (if any) differences there are between the create ...
134
votes
3
answers
166k
views
Should I use java.util.Date or switch to java.time.LocalDate?
Edit: Well, apparently it was too opinion based, so let me try to reword it more precisely -
Are there any clear caveats or drawbacks of using LocalDate, LocalTime etc. in Java code that does not need ...
132
votes
24
answers
266k
views
ORA-01882: timezone region not found
I'm accessing an Oracle Database from a java application, when I run my application I get the following error:
java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1
ORA-01882: ...
131
votes
15
answers
445k
views
java.sql.SQLException: - ORA-01000: maximum open cursors exceeded
I am getting an ORA-01000 SQL exception. So I have some queries related to it.
Are maximum open cursors exactly related to number of JDBC connections, or are they also related to the statement and ...
130
votes
14
answers
475k
views
What is the MySQL JDBC driver connection string?
I am new to JDBC and I am trying to make a connection to a MySQL database.
I am using Connector/J driver, but I cant find the JDBC connection string for my Class.forName() method.
130
votes
23
answers
285k
views
The infamous java.sql.SQLException: No suitable driver found
I'm trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps).
The app itself talks to Postgres just fine, so I know that the database is up, user ...
128
votes
11
answers
288k
views
Cannot issue data manipulation statements with executeQuery()
In MySQL I have two tables, tableA and tableB. I am trying to execute two queries:
executeQuery(query1)
executeQuery(query2)
But I get the following error:
can not issue data manipulation ...
126
votes
13
answers
207k
views
How to establish a connection pool in JDBC?
Can anybody provide examples or links on how to establish a JDBC connection pool?
From searching google I see many different ways of doing this and it is rather confusing.
Ultimately I need the code ...
126
votes
3
answers
109k
views
Closing JDBC Connections in Pool
Our standard code section for using JDBC is...
Connection conn = getConnection(...);
Statement stmt = conn.conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
...
125
votes
6
answers
244k
views
Multiple queries executed in java in single statement
Hi I was wondering if it is possible to execute something like this using JDBC as it currently provides an exception even though it is possible in the MySQL query browser.
"SELECT FROM * TABLE;INSERT ...
124
votes
3
answers
133k
views
What does java:comp/env/ do?
I just spent too much time of my day trying to figure out some errors when hooking up some JNDI factory bean. The problem turned out to be that instead of this...
<bean id="someId" class="org....
120
votes
2
answers
133k
views
Difference between oracle DATE and TIMESTAMP
What are the difference between Oracle DATE and TIMESTAMP type?
Both have date and time component?
Also what is corresponding type in Java for these date types?
119
votes
9
answers
467k
views
JDBC connection failed, error: TCP/IP connection to host failed
I want to connect Java class file with SQL server 2012. I have logged in with SQL server authentication, but I am receiving an error when connecting.
Error:
The TCP/IP connection to the host 127.0.0....
119
votes
18
answers
632k
views
How to convert TimeStamp to Date in Java?
How do I convert 'timeStamp' to date after I get the count in Java?
My current code is as follows:
public class GetCurrentDateTime {
public int data() {
int count = 0;
java.sql....
118
votes
22
answers
662k
views
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver MySQLConnector/J [duplicate]
What is wrong with the code there are lots of error while debugging. I am writing a code for a singleton class to connect with the database MySQL.
Here is my code
package com.glomindz.mercuri.util;
...
118
votes
21
answers
661k
views
How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate]
I am trying to create a connection to my database, when I put test my code using the main method, it works seamlessly. However, when trying to access it through Tomcat 7, it fails with error:
No ...
112
votes
15
answers
324k
views
Oracle JDBC ojdbc6 Jar as a Maven Dependency
I cannot seem to get Maven to bundle the ojdbc6.jar file into my project's war file. I have it working within the POM file when specifying a dependency directly for Hibernate tools. But it won't get ...