Sqlite JDBC 驱动示例

前端之家收集整理的这篇文章主要介绍了Sqlite JDBC 驱动示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


sqlite JDBC 驱动示例

JDBC驱动下载地址:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
import java.sql.Statement;

public class sqliteJDBC {

	public static void main(String[] args) {
		try {
			Class.forName("org.sqlite.JDBC");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection conn = null;
		try {
			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 (sqlException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}

猜你在找的Sqlite相关文章