系统环境:ubuntu10.04
1. 下载sqlite数据库第三方java扩展包
下载地址:
将其放至 $JAVA_HOME/lib 目录下(我的是在/usr/lib/jvm/java-6-openjdk/lib)
2. 设置环境变量
$ export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
#注意把新添加的JDK的bin目录追加在$PATH的最前面,这样当系统中安装有多个JDK版本时,程序会优先搜寻排在最前面的JDK版本。
$ export PATH=$JAVA_HOME/bin:$PATH
此处为把下载的sqlite数据库第三方java扩展包加入环境变量
$ export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/sqlite-jdbc-3.6.23.1-beta-1-SNAPSHOT.jar:$CLASSPATH
3. 测试用例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataBase_OP
{
public DataBase_OP()
{
System.out.println("it's database_op");
}
public Connection getConnection()
{
try
{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:data.db");
return conn;
}
catch(Exception e)
{
System.out.println("123exception");
e.printStackTrace();
return null;
}
}
public void searchAll()
{
try
{
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from save_data;");
int i = 0;
while (rs.next())
{
i++;
System.out.print("ID:" +i);
System.out.print("num: " + rs.getString("num"));
System.out.print("id: " + rs.getString("id"));
System.out.print("data: " + rs.getString("data"));
System.out.println("time: " + rs.getString("time"));
}
rs.close();
stat.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void searchById(String id)
{
try
{
Connection conn = getConnection();
PreparedStatement stat = conn.prepareStatement("select * from save_data where id=?;");
stat.setString(1, id);
ResultSet rs = stat.executeQuery();
int i = 0;
while (rs.next())
{
i++;
System.out.print("ID:" +i);
System.out.print("num: " + rs.getString("num"));
System.out.print("id: " + rs.getString("id"));
System.out.print("data: " + rs.getString("data"));
System.out.println("time: " + rs.getString("time"));
}
rs.close();
stat.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void deleteAll()
{
try
{
Connection conn = getConnection();
Statement stat = conn.createStatement();
stat.executeUpdate("delete from save_data;");
stat.close();
conn.close();
this.searchAll();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void deleteById(String id)
{
try
{
Connection conn = getConnection();
PreparedStatement stat = conn.prepareStatement("delete from save_data where id=?;");
stat.setString(1, id);
stat.executeUpdate();
stat.close();
conn.close();
this.searchAll();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void insert(String id,String data,String time)
{
try
{
Connection conn = this.getConnection();
PreparedStatement stat = conn.prepareStatement("insert into save_data values(null,?,?,?);");
stat.setString(1, id);
stat.setString(2,data);
stat.setString(3, time);
stat.executeUpdate();
stat.close();
conn.close();
this.searchById(id);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void createThread()
{
tsThread = new Thread(this,"db");
tsThread.start();
}
public void run()
{
if (tsThread != null)
{
this.searchAll();
}
}
public static void main(String args[])
{
DataBase_OP ts = new DataBase_OP();
ts.insert("2015", "1234", "2012-04-06");
ts.searchById("save_data","2015");
ts.deleteById("2015");
}
}
4. 编译运行
$ sqlite3 data.db //创建data.db 数据库,并建一张名了 save_data 的表
CREATE TABLE save_data(num integer primary key, id int, data text, time text);
$ javac DataBase_OP.java
$ java DatatBase
5.参考链接
http://ttitfly.iteye.com/blog/143934
阅读(8883) | 评论(0) | 转发(0) |