从mysql表读取时出错

时间:2014-03-05 14:51:23

标签: java mysql sql

我在数据库flight_data中创建了一个表FLIGHT_INFO(包含9列)。这个表的所有列都是VARCHAR(3or4or5)。虽然我能够在表中添加行(通过另一个java类),但我想编写一个能够读取该表的每一行的java类。 (注意:主要课程DBaseReader.readDB();)这是我的努力

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

public class DBaseReader {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/FLIGHT_INFO";

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

private static Connection conn = null;
private static Statement stmt = null;

public static void readDB() {

    String sql = "SELCET * FROM flight_data;"; //removing semi-colon doesn't solve problem
    ResultSet rs = null;

    String flightNo = null;
    String dep = null;
    String arr = null;
    String date = null;
    String flightTime = null;
    String flightDuration = null;
    String fare = null;
    String seat_aval = null;
    String flight_class = null;

    try {
        // STEP 2: Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // STEP 3: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        // STEP 4: Execute a query
        System.out.println("Creating statement...");
        stmt = conn.createStatement();

        rs = stmt.executeQuery(sql);//line 47 where error occurs
        // STEP 5: Extract data from result set

        while (rs.next()) {
            flightNo = rs.getString("flight_no");
            dep = rs.getString("dep");
            arr = rs.getString("arr");
            date = rs.getString("valid_till");
            flightTime = rs.getString("flight_time");
            flightDuration = rs.getString("flight_dur");
            fare = rs.getString("fare");
            seat_aval = rs.getString("seat_aval");
            flight_class = rs.getString("flight_class");

        }

        // Display values
        System.out.print("FLIGHT_NO: " + flightNo);
        System.out.print("DEP: " + dep);
        System.out.print("ARR: " + arr);
        System.out.println("VALID_TILL: " + date);
        System.out.print("FLIGHT_TIME: " + flightTime);
        System.out.print("FLIGHT_DUR: " + flightDuration);
        System.out.print("FARE" + fare);
        System.out.println("SEAT_AVAL: " + seat_aval);
        System.out.println("FLIGHT_CLASS: " + flight_class);

        stmt.close();
        conn.close();
    } catch (SQLException se) {
        // Handle errors for JDBC
        se.printStackTrace();
    } catch (Exception e) {
        // Handle errors for Class.forName
        e.printStackTrace();
    }

    try {
        if (conn != null)
            conn.close();
    } catch (SQLException se) {
        se.printStackTrace();
    }

}
}

但是我收到以下错误消息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELCET * FROM flight_data' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2537)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2466)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
at services.DBaseReader.readDB(DBaseReader.java:47)
at services.FlightSearchJDBC.main(FlightSearchJDBC.java:10)

那么如何成功地从表中读取?

1 个答案:

答案 0 :(得分:1)

呃...那么“SELECT”而不是“SELECT”呢? :)