Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.databases > #379

Re: java.sql.SQLException

From "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this>
Subject Re: java.sql.SQLException
Message-ID <nospam-F088A2.09255306122008@nntp.motzarella.org> (permalink)
Newsgroups comp.lang.java.databases
References <279af25b-bf8b-4920-bd38-be50ad24d563@n33g2000pri.googlegroup
Date 2011-04-27 15:23 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.databases
In article 
<279af25b-bf8b-4920-bd38-be50ad24d563@n33g2000pri.googlegroups.com>,
 Zhane <zhane84@gmail.com> wrote:

[...]
>    findFoodCode.setString(1,input);
>    findFoodCode.setString(2,request.getParameter("country"));
> 
>    ResultSet rs2 = findFoodCode.executeQuery();
>    ResultSetMetaData metaData = rs2.getMetaData();
> 
>    while(rs2.next()){
[...]

Presumably, findFoodCode is a PreparedStatement containing an UPDATE 
statement instead of a SELECT statement, as expected by executeQuery(). 
Different JDBC drivers may produce different error messages for this. An 
sscce <http://pscode.org/sscce.html> would be helpful.

For example, given the following DDL and Java:

<code>
create table customer(
  id integer, name varchar(10), phone varchar(10), last date);
insert into customer values(1, 'Jones', '2125551212', '2008-12-03');
insert into customer values(2, 'Smith', '2125551212', '2008-12-04');
insert into customer values(3, 'Wesson', '2125551212', '2008-12-05');

import java.sql.*;

/** @author John B. Matthews */
class Phone {
    public static void main (String args []) throws Exception {
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection
            ("jdbc:h2:tcp://localhost/src/java/jdbc/test", "sa", "");
        
        PreparedStatement ps = conn.prepareStatement(
            "SELECT name, phone, last FROM Customer WHERE name = ?");
        ps.setString(1, "Smith");
        ResultSet rset = ps.executeQuery();

        while (rset.next ()) {
            String name = rset.getString(1);
            String phoneNumber = rset.getString(2);
            String lastCall = rset.getDate(3).toString();
            System.out.println(name
                + " " + phoneFormat(phoneNumber)
                + " " + lastCall);
        }
    }

    private static String phoneFormat(String phone) {
        if (phone.length() == 10) return
            String.format("(%1$s) %2$s-%3$s",
                phone.substring(0, 3),
                phone.substring(3, 6),
                phone.substring(6));
        else return "Invalid";
    }
}
</code>

Execution produces the following output:

<console>
Smith (212) 555-1212 2008-12-04
</console>

-- 
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.databases | Previous | Next | Find similar


Thread

Re: java.sql.SQLException "John B. Matthews" <john.b..matthews@THRWHITE.remove-dii-this> - 2011-04-27 15:23 +0000

csiph-web