JTable

时间:2015-11-01 12:33:43

标签: java swing sqlite

您希望使用SQLite数据库创建JTable。数据应来自SQLite数据库并存储在此数据库中。现在,通过键入文本字段添加新的详细信息。但是,我收到错误消息:“java.sql.SQLException:near”(“:语法错误”

出了什么问题?

    public class SQLite     {

public static DefaultTableModel model = new DefaultTableModel();
private static JTextField fieldID;
private static JTextField fieldName;
private static JTextField fieldAge;
private static JTextField fieldAddress;
private static JTextField fieldSalary;

public static void main(String args []){

Connection c = null;
Statement stmt = null;
try {
  Class.forName("org.sqlite.JDBC");
  c = DriverManager.getConnection("jdbc:sqlite:test.db");
  c.setAutoCommit(false);
  System.out.println("Opened database successfully");

  stmt = c.createStatement();

  c.commit();

  JFrame f = new JFrame();
  f.setLayout(new GridLayout(5,1));
  f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

  fieldID = new JTextField("ID");
  fieldName = new JTextField("Name");
  fieldAge = new JTextField("Age");
  fieldAddress = new JTextField("Address");
  fieldSalary = new JTextField("Salary");

  String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " +
          "VALUES (fieldID.getText(), feldName.getText(), Age.getText(), feldAddress.getText(), feldSalary.getText()";
  stmt.executeUpdate(sql);

  JTable table = new JTable(model);
  f.add( new JScrollPane( table ) );

  model.addColumn("id");
  model.addColumn("name");
  model.addColumn("age");
  model.addColumn("address");
  model.addColumn("salary");

  ResultSet rs = stmt.executeQuery( "SELECT * FROM COMPANY;" );
  while ( rs.next() ) {
     int id = rs.getInt("id");
     String  name = rs.getString("name");
     int age  = rs.getInt("age");
     String  address = rs.getString("address");
     float salary = rs.getFloat("salary");

        model.addRow(new Object[] {id, name, age, address, salary});
  }

  /**JTextField feldID = new JTextField("ID");
  JTextField feldName = new JTextField("Name");
  JTextField feldAge = new JTextField("Age");
  JTextField feldAddress = new JTextField("Address");
  JTextField feldSalary = new JTextField("Salary");
    **/

  f.pack();
  f.setVisible( true );

  rs.close();
  stmt.close();
  c.close();
} catch ( Exception e ) {
  System.err.println( e.getClass().getName() + ": " + e.getMessage() );
  System.exit(0);
}
System.out.println("Operation done successfully");
}
}

2 个答案:

答案 0 :(得分:1)

这是你的问题:

"VALUES (fieldID.getText(), feldName.getText(), Age.getText(), feldAddress.getText(), feldSalary.getText()";

他们被视为litterals而不是getter的参考。

以下是您必须使用连接替换它的内容。

"VALUES ("+fieldID.getText()+","+feldName.getText()+","+Age.getText()+","+feldAddress.getText()+","+feldSalary.getText()+");";

答案 1 :(得分:1)

使用PreparedStatement。它更容易编码和维护,并将为您管理分隔符:

String sql = "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) VALUES (?, ?, ?, ?, ?)";

PreparedStatement stmt = connection.prepareStatement(sql);

stmt.setString( 1, fieldID.getText() );
stmt.setString( 2, fieldname.getText() );
stmt.setString( 3, ... );
stmt.setString( 4, ... );
stmt.setString( 5, ... );
stmt.executeUpdate();
  

但现在我得到java sql异常:“没有这样的列:ID”

嗯,这条消息是不言自明的。没有“ID”列。我们无法帮助您,因为只有您知道列的正确名称,因为您是创建数据库的人。