使用该类链接SQLite数据时需要下载sqlitejdbc-v056.jar,由于上传附件限制,所以jar包我没有上传下来,可以去百度搜索下载。代码如下:
package mainApp;
import java.sql.*;
public class SQLiteTest {
public static void main(String[] args) {
try {
// The SQLite (3.3.8) Database File
// This database has one table (pmp_countries) with 3 columns
// (country_id, country_code, country_name)
// It has like 237 records of all the countries I could think of.
String fileName = "E:/ProjectDocument/Citrus/syscity.db";
// Driver to Use
// http://www.zentus.com/sqlitejdbc/index.html
Class.forName("org.sqlite.JDBC");
// Create Connection Object to SQLite Database
// If you want to only create a database in memory, exclude the
// +fileName
Connection conn = DriverManager.getConnection("jdbc:sqlite:"+ fileName);
// Create a Statement object for the database connection, dunno what
// this stuff does though.
Statement stmt = conn.createStatement();
// Create a result set object for the statement
ResultSet rs = stmt
.executeQuery("SELECT * FROM citydb");
// Iterate the result set, printing each column
// if the column was an int, we could do rs.getInt(column name here)
// as well, etc.
while (rs.next()) {
String id = rs.getString("pk"); // Column 1
String name = rs.getString("cityname"); // Column 3
String ename = rs.getString("cityename");
System.out.println("id-->"+id+"&&name-->"+name+"&&ename-->"+ename);
/*if(ename==null || ename.equals("") || ename.length()<2){
ename = TestPinYin4j.getPinYin(name);
String sql = "update citydb set cityename = '"+ename+"' where pk="+id+";";
System.out.println("sql-->"+sql);
// stmt.executeQuery(sql);
}*/
}
// Close the connection
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载于:https://blog.51cto.com/yugangzhen/864454