Java操作sqlite数据库简单示例

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class DataDemo {
    public DataDemo() {
        try {
            Class.forName("org.sqlite.JDBC");// load the JDBC driver
            Connection conn = DriverManager.getConnection("jdbc:sqlite:D:\\javaworkspace\\EmployeeData.sqlite");// get

            System.out.println("OK!Connect successfully!");

            Statement stmt = conn.createStatement();
            String query = "select * from EmployeeInfo";
            ResultSet rs = stmt.executeQuery(query);// execute query
            java.sql.ResultSetMetaData rsmd = rs.getMetaData();

            int intColumns = rsmd.getColumnCount();

            System.out.println("ResultSet中的列数--" + intColumns);

            for (int i = 1; i <= intColumns; i++) {
                System.out.println(rsmd.getColumnLabel(i) + ((i == intColumns) ? "\n" : "\t"));
            }

            while (rs.next()) {
                System.out.print(rs.getString(1) + " ");
                System.out.println(rs.getString("name"));
            }
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, e);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    public static void main(String[] args) {
        new DataDemo();
    }
}

注意:Java和sqlite数据库连接的时候,需要用到这个jar文件 http://pan.baidu.com/s/1bPk5YI 还有,这个是示例中用到的数据库文件http://pan.baidu.com/s/1i5nYJ3J

你可能感兴趣的:(Java)