All Questions

Tagged with
Filter by
Sorted by
Tagged with
560 votes
7 answers
310k views

java.util.Date vs java.sql.Date

java.util.Date vs java.sql.Date: when to use which and why?
flybywire's user avatar
  • 267k
533 votes
35 answers
574k views

How to print a query string with parameter values when using Hibernate

Is it possible in Hibernate to print generated SQL queries with real values instead of question marks? How would you suggest to print queries with real values if it is not possible with Hibernate API?...
craftsman's user avatar
  • 15.4k
515 votes
17 answers
778k views

How to convert java.util.Date to java.sql.Date?

I am trying to use a java.util.Date as input and then creating a query with it - so I need a java.sql.Date. I was surprised to find that it couldn't do the conversion implicitly or explicitly - but ...
David Ackerman's user avatar
440 votes
5 answers
645k views

Hibernate show real SQL [duplicate]

if I set <property name="show_sql">true</property> in my hibernate.cfg.xml configuration file in the console I can see the SQL. But it's not real SQL... Can I see the SQL code that will ...
Tommaso Taruffi's user avatar
437 votes
15 answers
371k 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's user avatar
  • 8,246
320 votes
16 answers
569k 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's user avatar
  • 15.1k
194 votes
5 answers
223k 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 ...
Malax's user avatar
  • 9,516
194 votes
15 answers
327k 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 ...
froadie's user avatar
  • 81.5k
188 votes
13 answers
108k views

What are the pros and cons of performing calculations in sql vs. in your application

shopkeeper table has following fields: id (bigint),amount (numeric(19,2)),createddate (timestamp) Let's say, I have the above table. I want to get the records for yesterday and generate a report by ...
hellojava's user avatar
  • 4,984
185 votes
7 answers
414k 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 ...
lakshmi's user avatar
  • 4,739
169 votes
21 answers
8k views

Joins are for lazy people?

I recently had a discussion with another developer who claimed to me that JOINs (SQL) are useless. This is technically true but he added that using joins is less efficient than making several requests ...
Bastien Vandamme's user avatar
169 votes
16 answers
331k views

Java - escape string to prevent SQL injection

I'm trying to put some anti sql injection in place in java and am finding it very difficult to work with the the "replaceAll" string function. Ultimately I need a function that will convert any ...
Scott Bonner's user avatar
  • 2,930
159 votes
20 answers
408k views

SQLite in Android How to update a specific row

I've been trying to update a specific row for a while now, and it seems that there are two ways to do this. From what I've read and tried, you can just use the: execSQL(String sql) method or the: ...
EGHDK's user avatar
  • 18k
157 votes
10 answers
135k 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?
Prabhu R's user avatar
  • 14.1k
150 votes
7 answers
109k views

SQL parser library for Java [closed]

Is there an open-source Java library for parsing SQL statements? If possible, it should be customizable or flexible enough to also be able to parse (or at least ignore) vendor-specific syntax (such ...
Thilo's user avatar
  • 260k
136 votes
5 answers
345k views

what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity?

@Id @GeneratedValue(strategy = GenerationType.IDENTITY) Why we are using this annotations? i need to know if this autoincrement my table id values. (GenerationType.IDENTITY) is there any other types ...
Lijo's user avatar
  • 6,628
125 votes
16 answers
966k views

could not extract ResultSet in hibernate

I have a problem with Hibernate. I try to parse to List but It throws an exception: HTTP Status 500 - could not extract ResultSet. When I debug, It fault at line query.list()... My sample code here ...
Thanh Duy Ngo's user avatar
125 votes
15 answers
209k views

Most efficient conversion of ResultSet to JSON?

The following code converts a ResultSet to a JSON string using JSONArray and JSONObject. import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; import java.sql....
Devin Dixon's user avatar
124 votes
5 answers
146k views

Android Room @Delete with parameters

I know I can't use DELETE in a query (that is a shame by the way), I will get the following error: <i>Error:error: Observable query return type (LiveData, Flowable etc) can only be used with ...
Jack's user avatar
  • 1,545
123 votes
13 answers
187k views

Spring JPA @Query with LIKE

I'm trying to make a method in CrudRepository that will be able to give me list of users, whose usernames are LIKE the input parameter(not only begin with, but also contains it). I tried to use method ...
Viktoriia's user avatar
  • 2,340
123 votes
6 answers
242k 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 ...
MilindaD's user avatar
  • 7,613
122 votes
14 answers
172k views

Cleanest way to build an SQL string in Java

I want to build an SQL string to do database manipulation (updates, deletes, inserts, selects, that sort of thing) - instead of the awful string concat method using millions of "+"'s and quotes which ...
Vidar's user avatar
  • 6,613
120 votes
5 answers
162k views

Is there any good dynamic SQL builder library in Java? [closed]

Anyone knows some good SQL builder library for Java like Squiggle (not maintained anymore it seems). Preferably, a project in active development. Preferably with syntax like Zend_Db_Select, something ...
Vladislav Rastrusny's user avatar
116 votes
7 answers
171k views

Can I have H2 autocreate a schema in an in-memory database?

(I've already seen the H2 database In memory - Init schema via Spring/Hibernate question; it is not applicable here.) I'd like to know if there's a setting in H2 that will allow me to auto-create a ...
Laird Nelson's user avatar
  • 15.8k
113 votes
15 answers
34k views

Java Programming - Where should SQL statements be stored? [closed]

Where should an JDBC-compliant application store its SQL statements and why? So far, I managed to identify these options: Hardcoded in business objects Embedded in SQLJ clauses Encapsulate in ...
Adrian's user avatar
  • 6,051
105 votes
19 answers
325k views

How to call a stored procedure from Java and JPA

I am writing a simple web application to call a stored procedure and retrieve some data. Its a very simple application, which interacts with client's database. We pass employee id and company id and ...
user431514's user avatar
  • 1,191
103 votes
32 answers
169k views

The most sophisticated way for creating comma-separated Strings from a Collection/Array/List?

During my work with databases I noticed that I write query strings and in this strings I have to put several restrictions in the where-clause from a list/array/collection. Should look like this: ...
maerch's user avatar
  • 2,063
95 votes
11 answers
121k views

Storing UUID as base64 String

I have been experimenting with using UUIDs as database keys. I want to take up the least amount of bytes as possible, while still keeping the UUID representation human readable. I think that I have ...
mainstringargs's user avatar
91 votes
12 answers
217k views

Efficient way to do batch INSERTS with JDBC

In my app I need to do a lot of INSERTS. Its a Java app and I am using plain JDBC to execute the queries. The DB being Oracle. I have enabled batching though, so it saves me network latencies to ...
Aayush Puri's user avatar
  • 1,829
89 votes
4 answers
129k views

A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)

So far, I have not found a clear answer to this. I'd like to know what the equivalent is for a SQL type DATETIME and the java type, using a PreparedStatement. I have found: http://www.java2s.com/...
Stefan Hendriks's user avatar
87 votes
10 answers
114k views

handling DATETIME values 0000-00-00 00:00:00 in JDBC

I get an exception (see below) if I try to do resultset.getString("add_date"); for a JDBC connection to a MySQL database containing a DATETIME value of 0000-00-00 00:00:00 (the quasi-null value for ...
Jason S's user avatar
  • 187k
83 votes
9 answers
131k views

How to get SQL from Hibernate Criteria API (*not* for logging)

Is there a way to get the (to-be-generated) SQL from a Hibernate Criteria? Ideally, I would have something like: Criteria criteria = session.createCriteria(Operator.class); ... build up the ...
David Bulté's user avatar
  • 3,068
82 votes
2 answers
118k views

JPA 2 CriteriaQuery, using a limit

I am using JPA 2. For safety reasons, I am working type safe with CriteriaQuery's (and thus, I am not searching for any solutions to typed queries and so on). I recently came across an issue in which ...
Menno's user avatar
  • 12.4k
75 votes
1 answer
56k views

MySQL and JDBC with rewriteBatchedStatements=true

I've been reading around, here, here and here about the advantages of using rewriteBatchedStatements=true If I understood it right, with rewriteBatchedStatements=true the JDBC will pack as many ...
dazito's user avatar
  • 7,900
71 votes
13 answers
72k views

Running a .sql script using MySQL with JDBC

I am starting to use MySQL with JDBC. Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql:///x", "x", "x"); stmt = conn.createStatement(); stmt.execute( "CREATE ...
Macarse's user avatar
  • 92.5k
70 votes
5 answers
121k views

SQL JPA - Multiple columns as primary key

If i want severeal Column to make up an ID. SQL example : CONSTRAINT [PK_NAME] PRIMARY KEY ([Column1],[Column2],[Column3]) How can i do that with a Jpa Entity class ? through columndefinition ? ...
Anders Pedersen's user avatar
70 votes
7 answers
162k views

How do you access the value of an SQL count () query in a Java program

I want to get to the value I am finding using the COUNT command of SQL. Normally I enter the column name I want to access into the getInt() getString() method, what do I do in this case when there is ...
Ankur's user avatar
  • 50.7k
68 votes
8 answers
226k views

How to convert Calendar to java.sql.Date in Java?

Calendar cal; String sql = "INSERT INTO ttable (dt) values (?);" //dt is a dateTime field in ttable PreparedStatement stmt = connection.prepareStatement(sql); stmt = setDate(1,cal); //not working ...
Alex's user avatar
  • 3,335
68 votes
6 answers
298k views

Using setDate in PreparedStatement

In order to make our code more standard, we were asked to change all the places where we hardcoded our SQL variables to prepared statements and bind the variables instead. I am however facing a ...
roymustang86's user avatar
  • 8,260
67 votes
4 answers
107k views

Order By Date ASC with Spring Data

I try to make an application with Spring-Data-JPA on a table in order by ASC but it gives me an error: Invalid derived query! No property asc found for type java.util.Calendar Why ? List<Foo> ...
MaximeF's user avatar
  • 5,033
67 votes
4 answers
98k views

How to use a tablename variable for a java prepared statement insert [duplicate]

I am using a java PreparedStatment object to construct a series of batched INSERT queries. The query statement is of the format... String strQuery = "INSERT INTO ? (col1, col2, col3, col4, col5) ...
ForestSDMC's user avatar
66 votes
7 answers
92k views

MS SQL Exception: Incorrect syntax near '@P0'

I'm querying a DB using MS SQL and for some reason I get the following error: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0' even though this 'P0' isn't anywhere in my ...
user avatar
66 votes
10 answers
154k views

Hibernate Union alternatives

What alternatives do I have to implement a union query using hibernate? I know hibernate does not support union queries at the moment, right now the only way I see to make a union is to use a view ...
Miguel Ping's user avatar
  • 18.2k
55 votes
4 answers
143k views

JDBC ResultSet: I need a getDateTime, but there is only getDate and getTimeStamp

I would like to get the DATETIME column from an Oracle DB Table with JDBC. Here is my code: int columnType = rsmd.getColumnType(i); if(columnType == Types.DATE) { Date aDate = rs.getDate(i); ...
Tim's user avatar
  • 4,088
55 votes
9 answers
403k views

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname [duplicate]

I have this Java program: MySQLConnectExample.java import java.sql.*; import java.util.Properties; public class MySQLConnectExample { public static void main(String[] args) { Connection ...
user3416261's user avatar
55 votes
8 answers
68k views

JDBC ResultSet get columns with table alias

Imagine I have a query like SELECT * from table1 a, table2 b where (WHATEVER) Maybe both tables have the same column name. So I though it would be nice to access the data via resultSet.getString("...
Franz Kafka's user avatar
  • 10.8k
54 votes
3 answers
78k views

Best SQL browser for HSQLDB? [closed]

Whats the best browser tool for HSQLDB databases?
hawkeye's user avatar
  • 35.2k
54 votes
7 answers
30k views

Can I use enum parameter into JpaRepository nativeQuery?

Entity looks like this: @Getter @Setter @Entity public class Application { @Id private Long id; @Enumerated(EnumType.STRING) private ApplicationStatus status; } Code works this way: public ...
Tetiana Serediuk's user avatar
52 votes
2 answers
148k views

JPQL SELECT between date statement [closed]

I would like to convert this SQL statement to a JPQL equivalent. SELECT * FROM events WHERE events_date BETWEEN '2011-01-01' AND '2011-03-31'; This correctly retrieves the information from the table ...
Mamadou's user avatar
  • 577
52 votes
5 answers
184k views

Converting java date to Sql timestamp

I am trying to insert java.util.Date after converting it to java.sql.Timestamp and I am using the following snippet: java.util.Date utilDate = new java.util.Date(); java.sql.Timestamp sq = new java....
Shitu's user avatar
  • 817

1
2 3 4 5
373