import java.sql.*public class SQLiteJDBC{ public static void main( String args[] ) {Connection c = null try {//Class.forName加载class Class.forName("org.sqlite.JDBC") c = DriverManager.getConnection("jdbc:sqlite:test.db") } catch ( Exception e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ) System.exit(0) }System.out.println("Opened database successfully") }} 创建表import java.sql.*public class SQLiteJDBC{ public static void main( String args[] ) {Connection c = null Statement stmt = null try {//Class.forName加载class Class.forName("org.sqlite.JDBC") //DriverManager.getConnection创建连接 c = DriverManager.getConnection("jdbc:sqlite:test.db") System.out.println("Opened database successfully") stmt = c.createStatement() //sql创建表语句 String sql = "CREATE TABLE COMPANY " + "(ID INT PRIMARY KEY NOT NULL," + " NAME TEXTNOT NULL, " +" AGEINT NOT NULL, " +" ADDRESSCHAR(50), " +" SALARY REAL)" //executeUpdate创建表 stmt.executeUpdate(sql) stmt.close() c.close() } catch ( Exception e ) { System.err.println( e.getClass().getName() + ": " + e.getMessage() ) System.exit(0) }System.out.println("Table created successfully") }}
/*** @author Tim
*/
public class JavaSqlite {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// 0 连接SQLite的JDBC
String sql=\"jdbc:sqlite://e:/tim.db\"
Class.forName(\"org.sqlite.JDBC\")
// 1 建立一个数据库名zieckey.db的连接,如果不存在就在当前目录下创建之
Connection conn = DriverManager.getConnection(sql)
Statement stat = conn.createStatement()
// 2 创建一个表tbl1,录入数据
stat.executeUpdate(\"drop table if exists tbl1\")
stat.executeUpdate(\"create table if not exists tbl1(name varchar(20), salary int)\")// 创建一个表,两列
stat.executeUpdate(\"insert into tbl1 values(\'ZhangSan\',8000)\")// 插入数据
stat.executeUpdate(\"insert into tbl1 values(\'LiSi\',7800)\")
stat.executeUpdate(\"insert into tbl1 values(\'WangWu\',5800)\")
stat.executeUpdate(\"insert into tbl1 values(\'ZhaoLiu\',9100)\")
ResultSet rs = stat.executeQuery(\"select * from tbl1\")// 查询数据
System.out.println(\"创建表结构录入数据 *** 作演示:\")
while (rs.next()) { // 将查询到的数据打印出来
System.out.print(\"name = \" + rs.getString(\"name\") + \", \")// 列属性一
System.out.println(\"salary = \" + rs.getString(\"salary\"))// 列属性二
}
rs.close()
// 3 修改表结构,添加字段 address varchar(20) default \'changsha\'
stat.executeUpdate(\"alter table tbl1 add column address varchar(20) not null default \'changsha\'\")// 创建一个表,两列
stat.executeUpdate(\"insert into tbl1 values(\'HongQi\',9000,\'tianjing\')\")// 插入数据
stat.executeUpdate(\"insert into tbl1(name,salary) values(\'HongQi\',9000)\")// 插入数据
rs = stat.executeQuery(\"select * from tbl1\")// 查询数据
System.out.println(\"表结构变更 *** 作演示:\")
while (rs.next()) { // 将查询到的数据打印出来
System.out.print(\"name = \" + rs.getString(\"name\") + \", \")// 列属性一
System.out.print(\"name = \" + rs.getString(\"name\") + \", \")// 列属性二
System.out.println(\"address = \" + rs.getString(\"address\"))// 列属性三
}
rs.close()
conn.close()// 结束数据库的连接
} catch (Exception e) {
e.printStackTrace()
}
}
首先需要这个包,添加到工程。 下面给你示例代码 import java.sql.*public class SQLiteDemo { public static void main(String[] args){try{ //连接SQLite的JDBC Class.forName("org.sqlite.JDBC") //建立一个数据库名data.db的连接,如果不存在就在当前目录下创建之 Connection conn = DriverManager.getConnection("jdbc:sqlite:data.db") conn.setAutoCommit(false)Statement stat = conn.createStatement() stat.executeUpdate( "create table if not exists tbl1 (name varchar(20), salary int)" )//创建一个表,两列 stat.executeUpdate( "insert into tbl1 values('ZhangSan',8000)" )//插入数据 stat.executeUpdate( "insert into tbl1 values('LiSi',7800)" )stat.executeUpdate( "insert into tbl1 values('WangWu',5800)" )stat.executeUpdate( "insert into tbl1 values('ZhaoLiu',9100)" ) ResultSet rs = stat.executeQuery("select * from tbl1")//查询数据 while (rs.next()) { //将查询到的数据打印出来 System.out.print("name = " + rs.getString("name") + " ")//列属性一 System.out.println("salary = " + rs.getString("salary"))//列属性二 } rs.close()conn.commit()conn.close()//结束数据库的连接 }catch( Exception e ){ e.printStackTrace ( ) }}}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)