I get the "java.sql.SQLException: no such column: ID" in this Java code. I want to update a SQLite Database with the input of my TextFields. The output should be on a JTable. Could you say me, what´s wrong?
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()+","+fieldName.getText()+","+fieldAge.getText()+","+fieldAddress.getText()+","+fieldSalary.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");
} }
Aucun commentaire:
Enregistrer un commentaire