package sqlitetest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JFrame; import javax.swing.JTable; import javax.swing.JScrollPane; import javax.swing.table.DefaultTableModel; public class Sqlitetest { public static void main (String [] args) // TODO code application logic here throws Exception { Class.forName ("org.sqlite.JDBC"); String dburl = "jdbc: sqlite: /home/test/sqlitetest/Employees.db"; Connection connection = DriverManager.getConnection (dburl); Statement Query = connection.createStatement (); ResultSet rs = Query.executeQuery ("SELECT * FROM Employee"); DefaultTableModel model = new DefaultTableModel (); JTable table = new JTable (model); // I create 3 columns with their labels // these are the columns of the JTable modelo.addColumn ("CODE"); modelo.addColumn ("NAME"); modelo.addColumn ("DOMICILIO"); while (rs.next ()) { Object [] data = new Object [4]; // Create a vector // to store the ResultSet values data [0] = (rs.getInt (1)); data [1] = (rs.getString (2)); data [2] = (rs.getString (3)); System.out.println (rs.getString (2)); // I add the model to the table model.addRow (data); // data = null; // clean the data from the memory vector } rs.close (); // Close the ResultSet JFrame f = new JFrame (); f.set Bounds (10, 10, 300, 200); f.getContentPane (). add (new JScrollPane (table)); f.setVisible (true); } }
private void jButtonActionPerformed (java.awt.event.ActionEvent evt) { // TODO code application logic here try { Class.forName ("org.sqlite.JDBC"); String dburl = "jdbc: sqlite: /home/test/sqlitetest/Employees.db"; Connection connection = DriverManager.getConnection (dburl); String id = idtxt.getText (); String name = name.txt.getText (); String address = domicile.getText (); Statement Query = connection.createStatement (); Query.executeUpdate ("INSERT INTO Employee" + "VALUES (11, txtid 'Mr.', 'Springfield', 2001)"); connection.close (); }
Most popular questions within the last 30 days