您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 在Java下连接SQLite数据库
一、下载SQLite数据库的JDBC:二、将下载到的包解压后得到jar包放到%JAVA_HOME%\lib下,并且将其添加到ClassPath系统环境变量中。一定要保证在类路径ClassPath中有该jar包,并且保证在JAVA库路径JAVALibraryPath中有本地库NativeLibrary(\workspace\Web应用\WebRoot\WEB-INF\lib\下最好也要加入该jar包)。SQLite.JDBCDriver作为JDBC的驱动程序类名。连接JDBC的URL格式为jdbc:sqlite:/path。这里的path为指定到SQLite数据库文件的路径,例如:jdbc:sqlite://dirA/dirB/dbfilejdbc:sqlite://DRIVE:/dirA/dirB/dbfilejdbc:sqlite://COMPUTERNAME/shareA/dirB/dbfile三、下面是使用SQLite的两段代码以供参考:代码段1:1importjava.sql.*;2importorg.sqlite.JDBC;34publicclassSQLiteTest{5publicstaticvoidmain(String[]args){6try{7//TheSQLite(3.3.8)DatabaseFile8//Thisdatabasehasonetable(pmp_countries)with3columns(country_id,country_code,country_name)9//Ithaslike237recordsofallthecountriesIcouldthinkof.10StringfileName=c:/pmp.db;11//DrivertoUse12//(org.sqlite.JDBC);14//CreateConnectionObjecttoSQLiteDatabase15//Ifyouwanttoonlycreateadatabaseinmemory,excludethe+fileName16Connectionconn=DriverManager.getConnection(jdbc:sqlite:+fileName);17//CreateaStatementobjectforthedatabaseconnection,dunnowhatthisstuffdoesthough.18Statementstmt=conn.createStatement();19//Createaresultsetobjectforthestatement20ResultSetrs=stmt.executeQuery(SELECT*FROMpmp_countriesORDERBYcountry_nameASC);21//Iteratetheresultset,printingeachcolumn22//ifthecolumnwasanint,wecoulddors.getInt(columnnamehere)aswell,etc.23while(rs.next()){24Stringid=rs.getString(country_id);//Column125Stringcode=rs.getString(country_code);//Column226Stringname=rs.getString(country_name);//Column327System.out.println(ID:+id+Code:+code+Name:+name);2829}30//Closetheconnection31conn.close();32}33catch(Exceptione){34//Printsomegenericdebuginfo35System.out.println(e.getMessage());36System.out.println(e.toString());37}38}39}
本文标题:在Java下连接SQLite数据库
链接地址:https://www.777doc.com/doc-2562104 .html