当前位置: 代码迷 >> SQL >> java应用sqlite
  详细解决方案

java应用sqlite

热度:73   发布时间:2016-05-05 12:44:26.0
java使用sqlite
1.首先下载sqlite的驱动jar包,然后将其引入到工程中(驱动包会在文章后面附件给出 )
2.编写程序访问sqlite;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.sql.PreparedStatement;public class Sqlitetest {	public static void main(String args[]){	   try{	               //加载驱动		 Class.forName("org.sqlite.JDBC");	     //test.db为数据库的名字,如果有,就用该数据库,如果没有,则会自动创建该数据库              Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");	      Statement stat = conn.createStatement();	      stat.executeUpdate("drop table if exists people;");	      stat.executeUpdate("create table people (name, occupation);");	      PreparedStatement prep = conn.prepareStatement(	          "insert into people values (?, ?);");	      prep.setString(1, "Gandhi");	      prep.setString(2, "politics");	      prep.addBatch();	      prep.setString(1, "Turing");	      prep.setString(2, "computers");	      prep.addBatch();	      prep.setString(1, "Wittgenstein");	      prep.setString(2, "smartypants");	      prep.addBatch();	      conn.setAutoCommit(false);	      prep.executeBatch();	      conn.setAutoCommit(true);	      ResultSet rs = stat.executeQuery("select * from people;");	      while (rs.next()) {	          System.out.println("name = " + rs.getString("name"));	          System.out.println("job = " + rs.getString("occupation"));	      }	      rs.close();	      conn.close();	   }catch(Exception e){		   e.printStackTrace();	   }	}}
  相关解决方案