PreparedStatement « mysql « Java Database Q&A

1. Wildcards in Java PreparedStatements

Here's my current SQL statement:

SEARCH_ALBUMS_SQL = "SELECT * FROM albums WHERE title LIKE ? OR artist LIKE ?;";
It's returning exact matches to the album or artist names, but not anything else. ...

2. JDBC prepareStatement doesn't work

I'm trying to use the prepareStatement function. The code is below. After it executes, it returns me a bunch of string 'vlicense' instead of the the values. When the code finishing ...

3. JDBC, MySQL: getting bits into a BIT(M!=1) column

I'm new to using JDBC + MySQL. I have several 1/0 values which I want to stick into a database with a PreparedStatement. The destination column is a BIT(M!=1). I'm ...

4. Java Exception Error - Sqlite preparedStatement.setBlob

I'm placing and image into a databse, it could be eitehr an MYSQL database (the Server) or an SQLITE database (a Tablet PC for on the road). The Java application synchs ...

5. PreparedStatement throws MySQLSyntaxErrorException

I have the following Java code:

String query = "Select 1 from myTable where name = ? and age = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, name);
stmt.setInt(2, age);
ResultSet rs = stmt.executeQuery();
Whenever I run the ...

6. Is it expensive to hold on to PreparedStatements? (Java & JDBC)

I'm trying to figure out if it's efficient for me to cache all of my statements when I create my database connection or if I should only create those that are ...

7. Best Practices with PreparedStatements; when to and when not to

I recently have began using prepared statements again in a web application, and I know that it is discouraged to use prepared statements for all the transactions. What I do not ...

8. No server-side prepared statements using MySQL Connector/J

From what I understand, MySQL 5.1 supports server-side prepared statements. Therefore the following code should prepare the statement once, and execute it 10 times:

    Connection conn = ds.getConnection();

9. Set Auto-Increment into previous value JAVA PreparedStatement MYSQL

I have 3 buttons (add, save, cancel). If I press the add button, it automatically generates an auto-incremented value and is displayed in a text field. If I press the save ...

10. Problem with a prepared statement

I have this code:

 Date start = new Date(Integer.parseInt(jTextField4.getText()), Integer.parseInt(jTextField16.getText()), Integer.parseInt(jTextField17.getText()));  
        Date end = new Date(Integer.parseInt(jTextField5.getText()), Integer.parseInt(jTextField18.getText()), Integer.parseInt(jTextField19.getText()));

11. Time problem in prepared statement -> MySql

When I use the code below in a prepared statement

Date totalDate = visit.getTotalTime();
Date visitDate = visit.getVisitTime();

java.sql.Time sqlTotalTime = new java.sql.Time(totalDate.getTime() - visitDate.getTime()); 
ps.setTime(4, sqlTotalTime);
And I use these values
I would expect 00:00:02 ...

12. Consecutive PreparedStatement good practice

I'm executing a few SELECTs in a row and I'm wondering how I should handle the PreparedStatements. Example code:

//Connection conn is already declared
PreparedStatement pstmt = null;
ResultSet rset = null;
try {
  String ...

13. Parameter index out of range (2 > number of parameters, which is 1)

I wrote following java method and when I call it, gives me the error

Parameter index out of range (2 > number of parameters, which is 1).
Can anyone please explain me what's ...

14. JDBC PreparedStatement - Using the same argument, is it possible?

I'm using an "insert or update" query such as the one below:

        String sql = 

15. PreparedStatement No Value Specified for Parameter One

Having issues with this the one that needs to be set is a auto increment integer so how would I Specify that

PreparedStatement stmt = conn.prepareStatement("INSERT INTO `"+OnlineUsers.table2+"` VALUES (?,?,?)");


16. Multithreading - MySQL java connector prepared statement use

I have made a java class which runs in it's own thread that works through a queue of mysql queries so that it doesn't block the main application thread. I want ...

17. 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" ...

18. cast PreparedStatement to

hi all, i want to use the getLastInsertID() of mmMysql Driver because according to the author it is thread safe. in the documentation it was stated that i need to cast my prepared statement to inorder to use this specific function. how can i do it? i tried several statements but i can't get it right. pls. help. or if ...

20. MySQL 4.0.17 PreparedStatement Fails!

21. MySQL Prepared Statement & NULL

22. Prepared statement's 'setDate' method and MySQL execption

Hi Guys... I am new here. I was looking for a JDBC section in this forum, couldn't find one and this particular section seems the closest to what I was looking for, so I am posting here. I am listing down the full source code here: Java Code: import; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.annotation.Resource; import javax.servlet.ServletException; import ...

23. Problems with PreparedStatement and MySQL selecting bytes

And the test code to get the qtt_games is : conn = DriverManager.getConnection (url,user,pwd); byte bcode[] = poscode.getByteArrayCode(); // bcode is inserted ok in another preparedstatement... String query = "SELECT qtt_games FROM positions "+ "WHERE poscode=? and iswturn=?"; PreparedStatement pstmt = conn.prepareStatement(query); pstmt.setBytes (1,bcode); pstmt.setByte (2,poscode.getIsWhiteTurn()); //it returns a byte ResultSet rs = pstmt.executeQuery (query); When pstmt.executeQuery is reached, it's thrown ...