-2

I'm trying to create to the table into my database. And this is my code.. (my db name is koneksi, and my table name is table)

private void datatable(){
DefaultTableModel tbl = new DefaultTableModel();
tbl.addColumn("Nomor Induk");
tbl.addColumn("Nama Siswa");
tbl.addColumn("Kelas");
tbl.addColumn("Jenis Kelamin");
tbl.addColumn("Nilai Harian");
tbl.addColumn("Nilai UTS");
tbl.addColumn("Nilai UAS");
tbl.addColumn("Nilai Akhir");
tbl.addColumn("Grade");
tbl.addColumn("Keterangan");
table.setModel(tbl);
try{
Statement panggil = (Statement)koneksi.gettConnection().createStatement();
ResultSet res = panggil.executeQuery("select * from table");
while(res.next()){
    tbl.addRow(new Object[]{
    res.getString("nis"),
    res.getString("nama"),
    res.getString("kelas"),
    res.getString("jeniskelamin"),
    res.getString("harian"),
    res.getString("uts"),
    res.getString("uas"),
    res.getString("akhir"),
    res.getString("grade"),
    res.getString("ket"),
});
table.setModel(tbl);
}

}catch (Exception e)
{
JOptionPane.showMessageDialog(rootPane, e.getMessage());
   }
}

And this is my koneksi.java code..

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;


public class koneksi {
static Connection sambung;
public static Connection gettConnection(){
try{
    sambung=DriverManager.getConnection("jdbc:mysql://localhost/koneksi","root","");
}catch (Exception e){
    JOptionPane.showConfirmDialog(null,"Connection Failed");
}return sambung;
  }
}

I have to trying since 5 days ago, but I'm still getting this error..

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'table' at line 1.

I still learn Java basicly. I have been to asked my software engineering teacher at school how to fix this problem, but we still can't fix it. I'm waiting for ur solution answer, thanks. :)

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828

1 Answers1

3

table is a reserved keyword. See https://mariadb.com/kb/en/reserved-words/

You must either rename your table to some word that is not a reserved keyword, or else delimit it with back-ticks:

select * from `table`
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828