去maven下载驱动包 ( jar包 ) 下载地址:http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.23.1
打开IDEA
创建一个class类
package com.dao; import java.sql.*; public class DBHelp { //连接对象 static Connection conn =null; //命令对象 static Statement stmt=null; static PreparedStatement pstmt=null; //结果集 static ResultSet rs = null; public static void open(){ try { Class.forName("org.sqlite.JDBC");
//我这里的数据库是 在 E盘里的MyDB文件夹下 conn = DriverManager.getConnection("jdbc:sqlite:E:\MyDB\iphone.db"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public ResultSet getAll(String sql){ open(); try { stmt = conn.createStatement(); return rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return null; } public int delete(String sql){ open(); try { stmt = conn.createStatement(); return stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return 0; } public int insert(String sql,Object[] in){ open(); try { pstmt =conn.prepareStatement(sql); for(int i=0;i<in.length;i++) pstmt.setObject(i+1,in[i]); return pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return 0; } public int update(String sql,Object[] in){ open(); try { pstmt=conn.prepareStatement(sql); for(int i=0;i<in.length;i++){ pstmt.setObject(i+1,in[i]); } return pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return 0; } public static void close(){ try { if(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(conn!=null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
然后 开始你的测试吧。(♪(^∇^*))