首页 > 数据库技术 > 详细

【微型数据库SQlLite的测试】

时间:2016-05-25 02:23:31      阅读:298      评论:0      收藏:0      [点我收藏+]

仅仅需要引入驱动jar包即可进行数据库操作

?

package com.test.conn;

?

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

?

public class TestConn {

?

/**

* 微型数据库SQlLite的测试

* @param args

* @throws Exception?

*/

? ? ? ? ?public static void main(String[] args) throws Exception {

? ? ? ? ? ? ? ? ? ?Class.forName("org.sqlite.JDBC");

? ? ? ? ? ? ? ? Connection conn =

? ? ? ? ? ? ? ? ? ?DriverManager.getConnection("jdbc:sqlite:test.db");

? ? ? ? ? ? ? ? ? ?Statement stat = conn.createStatement();

? ? ? ? ? ? ? ? ? ?stat.executeUpdate("drop table if exists people;");

? ? ? ? ? ? ? ? ? ?stat.executeUpdate("create table IF NOT EXISTS 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();

?}

?

}


bubuko.com,布布扣
?

【微型数据库SQlLite的测试】

原文:http://gaojingsong.iteye.com/blog/2299358

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!