cc.
cc.

Reputation: 5633

JDBC - prepareStatement - How should I use it?

I saw this example somewhere:

 rs = connection.prepareStatement("select * from table").executeQuery();

Could I use this format, if I want to execute a query like this "Select * from table where column = "hello" "?

The way in which I usual I use prepareStatement object is something like this:

        String sql = "select * from adresa where column = ?";
        PreparedStatement pre = con.prepareStatement(sql);
        pre.setString(1, i);
        rs = pre.executeQuery();

Later Edit:

I don't understand. Pascal Thivent wrote that I can use the short version with In parameters, but Liu tells me this is not possible. :) Anw, using Pascal's version, i receive this error: void cannot be dereferenced

Upvotes: 3

Views: 26956

Answers (4)

Mona Jalal
Mona Jalal

Reputation: 38145

Here's a partial example how to use this interface:

static final String USER = "root";
            static final String PASS = "newpass";

            Connection conn = DriverManager.getConnection(myUrl, USER, PASS);

            // create a sql date object so we can use it in our INSERT statement
            Calendar calendar = Calendar.getInstance();
            java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());

            // the mysql insert statement
            String query = " insert into students (ID, last_name, first_name, birthday, hometown)"
                    + " values (?, ?, ?, ?, ?)";

            // create the mysql insert preparedstatement
            PreparedStatement preparedStmt = conn.prepareStatement(query);
            preparedStmt.setInt(1, 808027);
            preparedStmt.setString(2, "Davis");
            preparedStmt.setString(3, "Felicita");
            preparedStmt.setDate(4, startDate);
            preparedStmt.setString(5, "Venice");

            // execute the preparedstatement
            preparedStmt.execute();

            conn.close();

Upvotes: 5

Nathan Feger
Nathan Feger

Reputation: 19496

The long form is often, but prepared statements can be precompiled by the db, and if used properly will help prevent sql injection.

Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
 conn = getConn();
 ps = conn.prepareStatement("select * from x where y = ? "); //note no sb.append()'s or +'s, to helps prevent sql injection
 ps.setLong(1, 12l);
 rs = ps.executeQuery();

 while (rs.next()) {
 ... act ...
 }
} catch ( Exception e) {
} finally {
 if (rs != null) rs.close(); 
 if (ps != null) ps.close();
 if (conn != null) conn.close();
}

Who said java was verbose. :)

Upvotes: 0

Ken Liu
Ken Liu

Reputation: 22914

You can only use the first form if there are no bind variables (question marks) in the query. It's just a shortened version of what you posted.

Also, if you use the shortened form you won't have the opportunity to reuse the PreparedStatement object.

Upvotes: 1

Sabeen Malik
Sabeen Malik

Reputation: 10880

of course u can use a string variable for the query in which u put in ur dynamic data and run it.

rs = connection.prepareStatement(variable).executeQuery();

Upvotes: 0

Related Questions