Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 用PreparedStatement.executeQuery()时候报异常
micsolaris





发贴: 36
积分: 5
于 2008-11-10 21:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

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)
请问下如何解决啊


JiafanZhou edited on 2008-11-14 18:54


如何用JTable设置表头多行

话题树型展开
人气 标题 作者 字数 发贴时间
11930 用PreparedStatement.executeQuery()时候报异常 micsolaris 4384 2008-11-10 21:26
9632 Re:用PreparedStatement.executeQuery()时候报异常 JiafanZhou 611 2008-11-14 19:13
9758 Re:用PreparedStatement.executeQuery()时候报异常 micsolaris 130 2008-11-14 22:48
10154 Re:用PreparedStatement.executeQuery()时候报异常 JiafanZhou 70 2008-11-15 00:07

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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