0

I have created a Db(student.db3) with sqlite2009pro encrypted(password) protected.how to connect this to JTable.see the following code

   import java.sql.*;
   import javax.swing.*;

   public class javaconnect {

             Connection conn=null;
             public static Connection connecrDb(){
       try{

           Class.forName("org.sqlite.JDBC");
           Connection      conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\ME\\Desktop\\MY PROJECTS\\webcam\\sqlite3\\student.db3");
    //JOptionPane.showMessageDialog(null,"Connection Established");C:\Users\ME\Desktop\MY PROJECTS\Bunker Calcultaion Tool
     return conn;
    }catch ( Exception e){

        JOptionPane.showMessageDialog(null, e);
        return null;

    }


}

 }

and i am calling this class like,see the bellow code

public void Update_table() {

    try{
   conn=javaconnect.connecrDb();
   String sql="SELECT * FROM grade";

    pst=conn.prepareStatement(sql);
   rs=pst.executeQuery();
  jTable1.setModel(DbUtils.resultSetToTableModel(rs));


}
catch( Exception e){

        JOptionPane.showMessageDialog(null, "problem exists");

   }
}
akathir79
  • 31
  • 4

1 Answers1

0

One way to do this would be to have the DbUtils.resultSetToTableModel method return an instance of javax.swing.table.DefaultTableModel.

You could construct the DefaultTableModel with this constructor:

DefaultTableModel(Vector data, Vector columnNames)

The data Vector contains all the rows. Each row is itself a Vector that contains the field values in each row.

So you could use code similar to the following:

public static TableModel resultSetToTableModel(ResultSet rs) {
    Vector data = new Vector();
    while (rs.next()) {
        Vector row = new Vector();
        row.add(rs.getObject(1));
        row.add(rs.getObject(2));
        ... repeat for each field...

        data.add(row);
    }         

    Vector columnNames = new Vector();
    columnNames.add("columnName1");
    columnNames.add("columnName2");
    ... repeat for each field...

   TableModel result = new DefaultTableModel(data, columnNames);

   return result;
}
  • above code cannot connect encrypted(password protected ) database.please once again read my question. – akathir79 May 19 '16 at 22:09
  • Ok, based on the title of the question I assumed this was about using JDBC with JTable. If the problem is that you cannot connect to the database at all, then have a look at these: http://stackoverflow.com/questions/12803140/java-jdbc-driver-sqlite-3-7-2-unable-to-open-database-test-db-file-is-encry http://stackoverflow.com/questions/27218985/how-to-set-database-password-in-sqlite-jdbc https://gist.github.com/ggiraldez/7141132 Basically it seems that the standard SQLite JDBC driver does not support encryption. – Karl Harbour May 22 '16 at 16:24