Topic: 用PreparedStatement.executeQuery()时候报异常

  Print this page

1.用PreparedStatement.executeQuery()时候报异常 Copy to clipboard
Posted by: micsolaris
Posted on: 2008-11-10 21:26


package com.ricky.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class StatementTest {
  Connection conn = null;
  Statement stmt = null;
  PreparedStatement pstmt = null;

  public static void main(String[] args) throws SQLException {
    StatementTest SQLExample = new StatementTest();
    SQLExample.doStatement();
    SQLExample.doPreparedStatement();

    SQLExample.conn.close();

  }

  public StatementTest() {
    String sourceURI = "jdbc:odbc:technical_library";
    String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";

    System.setProperty("jdbc.drivers", driverClass);
    try {
      conn = DriverManager.getConnection(sourceURI);
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void doStatement() throws SQLException {

    stmt = conn.createStatement();

    String sql = "SELECT * FROM authors";

    ResultSet rs = null;

    rs = stmt.executeQuery(sql);

    showResultSet(rs);

    stmt.close();

    // rs.close();

  }

  public void doPreparedStatement() throws SQLException {
    String sql = "SELECT authid,lastname,fristname FROM authors ORDER BY authid";

    pstmt = conn.prepareStatement(sql);

    ResultSet rs = null;

    rs = pstmt.executeQuery();

    showResultSet(rs);

    pstmt.close();

    // rs.close();
  }

  public void showResultSet(ResultSet rs) throws SQLException {
    ResultSetMetaData metadata = null;

    metadata = rs.getMetaData();

    int columns = 0;

    columns = metadata.getColumnCount();

    System.out
        .println("-------------------------Execute Result-------------------------");

    while (rs.next()) {
      System.out.printf("%-5d", rs.getInt(1));
      for (int i = 2; i < columns; i++) {
        System.out.print(rs.getString(i) + " ");
      }
      System.out.println();
    }

    System.out
        .println("----------------------------Query Metadata----------------------------");

    System.out.println("ResultSet contains " + columns + " columns");

    for (int i = 1; i < columns; i++) {
      System.out.printf(
          "%-3d" + " : " + metadata.getColumnName(i) + "\n", i);
    }

    rs.close();

  }
}


<--------------------------------------------------------------------->
Exception in thread "main" java.sql.SQLException:
Microsoft ODBC Microsoft Access Driver 参数不足,期待是 1。
  at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
  at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
  at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
  at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
  at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeQuery(Unknown Source)
  at com.ricky.test.StatementTest.doPreparedStatement(StatementTest.java:62)
  at com.ricky.test.StatementTest.main(StatementTest.java:19)
请问下如何解决啊

2.Re:用PreparedStatement.executeQuery()时候报异常 [Re: micsolaris] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-11-14 19:13

Using PreparedStatement object can send parameterized SQL statement to the
database. I am not sure what will happen if you create a PreparedStatement
without any parameters.

It is important to note that PreparedStatenment will require the driver to
support precompilation. Some drivers may not support precompilation. In this case,
it does affect which methods throw certain SQLException object.

I am currently working on a Linux machine, thus I cannot retest your program
at the moment. I do have a windows machine at home, and I will be likely to test
your code at the weekend.

Thanks,
Jiafan

3.Re:用PreparedStatement.executeQuery()时候报异常 [Re: micsolaris] Copy to clipboard
Posted by: micsolaris
Posted on: 2008-11-14 22:48

谢谢JiafanZhou
我发现我错在哪里了,我把 String sql = "SELECT authid,lastname,fristname FROM authors ORDER BY authid";中的firstname写错了,所以出现了异常。

4.Re:用PreparedStatement.executeQuery()时候报异常 [Re: micsolaris] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-11-15 00:07

Cool... this is exactly why Gavin invented the Hibernate.!! Smile
Jiafan


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923