Questions tagged [jdbc]

11373 questions
462

votes
5

answer
258.8k

Views

java.util.Date vs java.sql.Date

java.util.Date vs java.sql.Date: when to use which and why?
flybywire
340

votes
11

answer
240.3k

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?
Satya
307

votes
27

answer
282.3k

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 ? placeholder represents one value, rather than a list of values. Consider the following SQL statement: SE...
Chris Mazzola
301

votes
15

answer
187.2k

Views

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 in blogs and other forums but could not reach a decis...
Dema
291

votes
14

answer
309.4k

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] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Dr...
mona
280

votes
12

answer
471.6k

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(DriverManager.java:689) at java.sql.DriverManager.getConnection(DriverManager.java:247) Or java.lang.ClassNotFou...
abson
269

votes
21

answer
379.2k

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'); }
kal
259

votes
16

answer
334.8k

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: com.oracle ojdbc14 10.2.0.3.0 of course this does't work as it is not in the central repository used by maven. 2 questions: How do I find a repositor...
rperez
256

votes
4

answer
119.4k

Views

Spring - @Transactional - What happens in background?

I want to know what actually happens when you annotate a method with @Transactional? Of course, I know that Spring will wrap that method in a Transaction. But, I have the following doubts: I heard that Spring creates a proxy class? Can someone explain this in more depth. What actually resides in tha...
peakit
252

votes
14

answer
389.9k

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.
Jake
214

votes
8

answer
162.4k

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; PreparedStatement stmt = null; ResultSet rs = null; try { conn = // Retrieve connection stmt = conn.prepareStatem...
Zeemee
207

votes
7

answer
475k

Views

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 to a different Oracle database that does not use a...
Jim Tough
192

votes
14

answer
216.3k

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 relational databases handles a JDBC / SQL query in four steps: Parse the incoming SQL query Compil...
SO_Bee
191

votes
10

answer
273.6k

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.
169

votes
30

answer
462.9k

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' com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet...
Josh K
165

votes
25

answer
525k

Views

Solving a “communications link failure” with JDBC and MySQL [duplicate]

This question already has an answer here: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 30 answers 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) { Co...
Anthony
156

votes
13

answer
225.2k

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 when tyring to add a row to a table: Incorrect string value: '\xF0\x90\x8D\x83\xF0\x90...' for column 'c...
Lior
152

votes
5

answer
144k

Views

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

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 = jobTypes[i]; if(i != 0) { jobTypeInClauseBuilder.app...
Malax
151

votes
13

answer
180.6k

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 to Mysql JDBC driver 5.1.33. After the upgrade, Tomcat would throw this error when starting the app....
bluecollarcoder
140

votes
8

answer
237.5k

Views

Get query from java.sql.PreparedStatement [duplicate]

This question already has an answer here: How can I get the SQL of a PreparedStatement? 13 answers 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...
llm
138

votes
16

answer
59.7k

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 notification when the request will return a value (by passing a Listener/callback or something). I don't...
Steve Gury
135

votes
5

answer
135.7k

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. And I have also used prepared statements before, but I do not know how to use it with LIKE because fro...
ssn
131

votes
13

answer
206.2k

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 the sql statement and the parameters in the queryParams variable length array, runs it, caches the Res...
froadie
130

votes
12

answer
87.6k

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, the date value is auto assigned to 0000-00-00 00:00:00 when the table data is called with the date co...
121

votes
4

answer
82.3k

Views

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 = DriverManager.getConnection(myConnectionURL); PreparedStatement ps = con.prepareStatement(sql);...
Jonas
117

votes
7

answer
119.8k

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?
marcosbeirigo
105

votes
3

answer
105.6k

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... I had actually written this... I infer that the java:comp/env/ perhaps references some environment variable and makes it so that, ultima...
Danny
103

votes
8

answer
321.1k

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 port number. Where do I find all of these?
102

votes
10

answer
298.7k

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.
101

votes
13

answer
168k

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 to return a java.sql.Connection object, but I am having trouble getting started..any suggestions welc...
llm
97

votes
6

answer
75.4k

Views

JdbcTemplate queryForInt/Long is deprecated in Spring 3.2.2. What should it be replaced by?

The queryforInt/queryforLong methods in JdbcTemplate are deprecated in Spring 3.2. I can't find out why or what is considered the best practice to replace existing code using these methods. A typical method: int rowCount = jscoreJdbcTemplate.queryForInt( 'SELECT count(*) FROM _player WHERE nameKey =...
Dan MacBean
96

votes
3

answer
76.6k

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, ResultSet.CONCUR_READ_ONLY); ResultSet rset = stmt.executeQuery (sqlQuery); // do stuff with rset rset.close(); stmt.close(); conn.clo...
Manidip Sengupta
95

votes
13

answer
267.9k

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 resultset objects we have created for a single connection ? (We are using pool of connections) Is there...
Kanagavelu Sugumar
94

votes
9

answer
79.6k

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 will be a string or otherwise?
Ram
92

votes
2

answer
91.4k

Views

Reusing a PreparedStatement multiple times

in the case of using PreparedStatement with a single common connection without any pool, can I recreate an instance for every dml/sql operation mantaining the power of prepared statements? I mean: for (int i=0; i
Steel Plume
92

votes
8

answer
580.8k

Views

java.net.SocketException: Connection reset

I am working on a spring related application using PostgreSQL. While I am trying to click the tabs in UI, I am getting this error. I know this is old question which is asked by so many people but those answers couldn't help to me. So I am raising this question again Please help me. SEVERE: Servlet....
mahesh chinta
87

votes
15

answer
404.7k

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.Timestamp timeStamp = new Timestamp(System.currentTimeMillis()); java.sql.Date date = new java.sql.Date(timeStamp.getTime()...
Krishna Veni
87

votes
6

answer
126.2k

Views

MySQL 'create schema' and 'create database' - Is there any difference

Taking a peak into the information_schema database and peaking 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 schema command and the create database command for MySQL. Are there any differences? If not, is this...
Bob
86

votes
5

answer
59.2k

Views

Spring DAO vs Spring ORM vs Spring JDBC

I was going through data access technologies supported by Spring, and I noticed that it mentions multiple options and I am not sure about the difference among them: Spring-DAO (http://docs.spring.io/spring/docs/2.0.8/reference/dao.html) Spring-ORM (http://docs.spring.io/spring/docs/3.0.x/spring-fram...
Pat
86

votes
4

answer
71.6k

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. I opened a dynamic web project, created a session EJB , I used EntityManager and with the get meth...
user907810