SQLite:Java操作SQLite

前端之家收集整理的这篇文章主要介绍了SQLite:Java操作SQLite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_502_1@1.sqliteJDBC

sqliteJDBCDriver可以在这个网站下载https://bitbucket.org/xerial/sqlite-jdbc/overview,当前稳定版本sqlite-jdbc-3.7.2.jar

@H_502_1@2. Java 代码

添加sqlite-jdbc-3.7.2.jar,与你添加其他jar包的方法一样。

[java] view plain copy
  1. importjava.sql.Connection;
  2. importjava.sql.DriverManager;
  3. importjava.sql.ResultSet;
  4. importjava.sql.sqlException;
  5. importjava.sql.Statement;
  6. publicclasssqliteTest
  7. {
  8. staticvoidmain(String[]args)throwsClassNotFoundException
  9. //loadthesqlite-JDBCdriverusingthecurrentclassloader
  10. Class.forName("org.sqlite.JDBC");
  11. Connectionconnection=null;
  12. try
  13. //createadatabaseconnection
  14. connection=DriverManager.getConnection("jdbc:sqlite:sample.db");
  15. Statementstatement=connection.createStatement();
  16. statement.setQueryTimeout(30);//settimeoutto30sec.
  17. statement.executeUpdate("droptableifexistsperson");
  18. statement.executeUpdate("createtableperson(idinteger,namestring)");
  19. statement.executeUpdate("insertintopersonvalues(1,'leo')");
  20. statement.executeUpdate("insertintopersonvalues(2,'yui')");
  21. ResultSetrs=statement.executeQuery("select*fromperson");
  22. while(rs.next())
  23. //readtheresultset
  24. System.out.println("name="+rs.getString("name"));
  25. System.out.println("id="+rs.getInt("id"));
  26. }
  27. }
  28. catch(sqlExceptione)
  29. {
  30. //iftheerrormessageis"outofmemory",
  31. //itprobablymeansnodatabasefileisfound
  32. System.err.println(e.getMessage());
  33. finally
  34. try
  35. if(connection!=null)
  36. connection.close();
  37. catch(sqlExceptione)
  38. //connectioncloseFailed.
  39. System.err.println(e);
  40. }
参考资料:https://bitbucket.org/xerial/sqlite-jdbc/overview

猜你在找的Sqlite相关文章