兩天從0基礎寫的。沒有按鈕對話框功能,只是簡單的實現。html
固然代碼上有不少須要優化的,基本須要重寫哈哈哈。可是我怕之後有須要因此仍是存一下好了。《把RS結果集,放vector裏面,用vector構造JTable》java
1 package meterSqllitedataFinal; 2 3 import java.awt.Dimension; 4 import java.util.Vector; 5 import javax.swing.JButton; 6 import javax.swing.JFrame; 7 import javax.swing.JLabel; 8 import javax.swing.JScrollPane; 9 import javax.swing.JTable; 10 11 public class mainn extends JFrame { 12 //private Vector rowData, columnName; 13 private JTable jt = null; 14 private JScrollPane jsp = null; 15 private JButton jb = null; 16 17 public static void main(String[] args) { 18 // 顯示應用 GUI 19 mainn miann1 = new mainn(); 20 } 21 22 public mainn() 23 { 24 sqlitedata a = new sqlitedata(); 25 //初始化JTable 26 a.mainjj(); 27 28 jt = new JTable(a.rowData, a.columnName); 29 30 jsp = new JScrollPane(jt); 31 32 this.add(jsp); 33 this.setTitle("by:Zing莊 SqlliteConnect"); 34 this.setSize(460, 200); //這是大小 35 this.setLocation(300, 200); //這是位置 36 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 this.setResizable(true); //尺寸是否可變 38 this.setVisible(true); //顯示與隱藏 39 } 40 41 }
1 package meterSqllitedataFinal; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.ResultSetMetaData; 7 import java.sql.Statement; 8 import java.util.Vector; 9 10 import javax.swing.JScrollPane; 11 import javax.swing.JTable; 12 13 import org.omg.CORBA.PRIVATE_MEMBER; 14 15 public class sqlitedata { 16 public Vector rowData, columnName; 17 18 public void mainjj() { 19 Connection c = null; 20 Statement stmt = null; 21 22 rowData = new Vector(); 23 try { 24 Class.forName("org.sqlite.JDBC"); 25 c = DriverManager.getConnection("jdbc:sqlite:d:\\web\\data.data\\");//tim.data 26 c.setAutoCommit(false); 27 System.out.println("Opened database successfully"); 28 29 stmt = c.createStatement(); 30 ResultSet rs = stmt.executeQuery("SELECT * FROM Meter;");//COMPANY 31 ResultSetMetaData data = rs.getMetaData(); 32 33 columnName = new Vector(); 34 System.out.println("dd"); 35 36 37 38 for (int i = 1; i <= data.getColumnCount(); i++) { 39 columnName.add(data.getColumnName(i));//這裏是列名 40 } 41 42 while (rs.next()) { 43 44 Vector line1 = new Vector(); 45 for (int k = 1; k <= data.getColumnCount(); k++) { 46 47 line1.add(rs.getString(data.getColumnName(k)));//這裏是添加行數據 48 } 49 rowData.add(line1); 50 } 51 52 rs.close(); 53 stmt.close(); 54 c.close(); 55 } catch (Exception e) { 56 System.err.println(e.getClass().getName() + ": " + e.getMessage()); 57 System.exit(0); 58 } 59 System.out.println("Operation done successfully"); 60 61 } 62 63 }
須要導入的驅動web
項目須要導入sql驅動。build Path →Configure Build Path →Add External JARs
http://blog.csdn.net/qq_21478795/article/details/51579181
這個是借鑑的地方。
https://zhidao.baidu.com/question/2202449700556627748.html
這個借鑑如何傳輸進數據
http://blog.csdn.net/a412588063/article/details/21242887
這沒借鑑過,可是挺詳細的。
http://www.runoob.com/sqlite/sqlite-java.html
這裏是sqllite驅動下載