java.sql.SQLSyntaxErrorException:语法错误:在第1行第15列遇到“Book”

时间:2017-10-13 22:34:55

标签: java jdbc derby

package javaapplication2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



//jdbc:derby://localhost:1527/LMS


class abc {


public  abc() throws SQLException{


 //ConnectionURL, username and password should be specified in getConnection()


 //ConnectionURL, username and password should be specified in getConnection()
 try {String url = "jdbc:derby://localhost:1527/LMS";
            Connection conn = DriverManager.getConnection(url,"zain","12345");
  System.out.println("Connected! ");

  String sql = "SELECT * FROM BOOK";`enter code here`
 Statement st = conn.createStatement();
 ResultSet rs=st.executeQuery(sql);
 String x;
 while(rs.next()){


System.out.println(rs.getString("Password")); 
 }


 } 


 catch (SQLException ex) {
 System.out.println(ex);
 }


} 
}

我得到这个任何领先?我正在尝试这个东西几个小时而没有得到任何领先:/ plz帮助。我正在制作一个图书馆管理系统并为其制作了一本Db,其中包含一本专栏文章并尝试从中获取数据。

1 个答案:

答案 0 :(得分:-1)

德比是一个棘手的野兽。 这是一个数据retreivale的工作示例和准备插入Jtable

public static final String DRIVER= "org.apache.derby.jdbc.EmbeddedDriver";
public static final String JDBC_URL = "jdbc:derby://localhost:1527/LMS";

private  void GetData()throws  SQLException 
{
    Connection connection=DriverManager.getConnection(JDBC_URL,"zain","12345");
    ResultSet result = null; 
    String sql2 = "select * from book"; 
    PreparedStatement ps1 = connection.prepareStatement( sql2, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY) ;
    result = ps1.executeQuery();
    // get number of rows
    result.last();
    int numRows = result.getRow();
    result.first();
    //Get metadata object for result
    ResultSetMetaData meta = result.getMetaData();
    // create an arry of string for the colum names
    colNames = new String[meta.getColumnCount()];
    // store column names in the new col names array
    for( int i = 0; i< meta.getColumnCount(); i++)
    {
        //get column name
        colNames[i] = meta.getColumnLabel(i+1);

    }
    // create 2 d string array for table data
    tableData = new String [numRows][meta.getColumnCount()];
    // store columns in the data
    for ( int row = 0 ; row < numRows; row++)
    {
        for (int col = 0; col < meta.getColumnCount(); col++)
        {
            tableData[row][col]= result.getString(col + 1);
        }
        result.next();
    }
    // close statement
    ps1.close();
    connection.close();
} // end get data 

插入一个gui ...

 table = new JTable(tableData,colNames);
 JScrollPane  scrollPane = new JScrollPane(table);