jdbc怎么用代码创建sqlite数据库
2017-02-13 · 知道合伙人软件行家
关注
展开全部
示例程序如下:
import java.sql.*;
import org.sqlite.JDBC;
public class TestSQLite
{
public static void main(String[] args)
{
try
{
//连接SQLite的JDBC
Class.forName("org.sqlite.JDBC");
//建立一个数据库名zieckey.db的连接,如果不存在就在当前目录下创建之
Connection conn = DriverManager.getConnection("jdbc:sqlite:aa.db");
Statement stat = conn.createStatement();
stat.executeUpdate( "create table 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.close(); //结束数据库的连接
}
catch( Exception e )
{
e.printStackTrace ( );
}
}
}
import java.sql.*;
import org.sqlite.JDBC;
public class TestSQLite
{
public static void main(String[] args)
{
try
{
//连接SQLite的JDBC
Class.forName("org.sqlite.JDBC");
//建立一个数据库名zieckey.db的连接,如果不存在就在当前目录下创建之
Connection conn = DriverManager.getConnection("jdbc:sqlite:aa.db");
Statement stat = conn.createStatement();
stat.executeUpdate( "create table 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.close(); //结束数据库的连接
}
catch( Exception e )
{
e.printStackTrace ( );
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询