package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author 作者:tony chan
* @email 邮箱:chenzs@tedu.cn
* @version v.1.0 创建时间:上午8:45:59
* @description 描述:java链接sqlite轻型数据库
*/
public class TestSqlite {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String driver = "org.sqlite.JDBC";
String db = "C:\\Qt\\ws\\StudentSys\\stu.db";
String url = "jdbc:sqlite:" + db; //注意SID
String sql = "select * from tb_student";
Class.forName(driver);
Connection cn = DriverManager.getConnection(url);
Statement stat = cn.createStatement();
ResultSet rs = stat.executeQuery(sql); //执行SQL语句返回结果集
ResultSetMetaData md = rs.getMetaData(); //获取表结构信息
//获取列数
int cols = rs.getMetaData().getColumnCount();
//打印列名
for(int i=1; i<=cols; i++) {
System.out.print(md.getColumnName(i)+"\t");
}
//打印数据
while(rs.next()) {
System.out.println();
for(int i=1; i<=cols; i++) {
System.out.print(rs.getString(i)+"\t\t");
}
}
}
}