两天从0基础写的。没有按钮对话框功能,只是简单的实现。
当然代码上有很多需要优化的,基本需要重写哈哈哈。但是我怕以后有需要所以还是存一下好了。《把RS结果集,放vector里面,用vector构造JTable》
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 }
需要导入的驱动
项目需要导入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驱动下载