· 8 years ago · Jan 19, 2018, 01:40 AM
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.sql.Statement;
6
7public class Sample
8{
9 public static void main(String[] args) throws ClassNotFoundException
10 {
11 // load the sqlite-JDBC driver using the current class loader
12 Class.forName("org.sqlite.JDBC");
13
14 Connection connection = null;
15 try
16 {
17 // create a database connection
18 connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
19 Statement statement = connection.createStatement();
20 statement.setQueryTimeout(30); // set timeout to 30 sec.
21
22 statement.executeUpdate("drop table if exists person");
23 statement.executeUpdate("create table person (id integer, name string)");
24 statement.executeUpdate("insert into person values(1, 'leo')");
25 statement.executeUpdate("insert into person values(2, 'yui')");
26 ResultSet rs = statement.executeQuery("select * from person");
27 while(rs.next())
28 {
29 // read the result set
30 System.out.println("name = " + rs.getString("name"));
31 System.out.println("id = " + rs.getInt("id"));
32 }
33 }
34 catch(SQLException e)
35 {
36 // if the error message is "out of memory",
37 // it probably means no database file is found
38 System.err.println(e.getMessage());
39 }
40 finally
41 {
42 try
43 {
44 if(connection != null)
45 connection.close();
46 }
47 catch(SQLException e)
48 {
49 // connection close failed.
50 System.err.println(e);
51 }
52 }
53 }
54}
55
56# rem set PATH=C:ProgramDataOracleJavajavapath;%PATH%
57# set CLASSPATH=C:sqlitejavasqlite-jdbc-3.21.0.jar
58# C:jython2.7.0binjython.exe sample.py
59
60import java
61from java.sql import SQLException
62#load the sqlite-JDBC driver using the current class loader
63java.lang.Class.forName("org.sqlite.JDBC")
64url = "jdbc:sqlite:C:/sqlite/db/chinook.db"
65connection = None
66try:
67 connection = java.sql.DriverManager.getConnection(url)
68 statement = connection.createStatement()
69 statement.executeUpdate("drop table if exists person")
70 statement.executeUpdate("create table person (id integer, name string)");
71 statement.executeUpdate("insert into person values(1, 'leo')");
72 statement.executeUpdate("insert into person values(2, 'yui')");
73 rs = statement.executeQuery("select * from person")
74 while (rs.next()):
75 print("name = %s"%(rs.getString("name")))
76 print("id = %s"%(rs.getString("id")))
77except SQLException as e:
78 # if the error message is "out of memory",
79 # it probably means no database file is found
80 print("error: %s"%e.getMessage())
81finally:
82 try:
83 if(connection is not None):
84 connection.close()
85 except SQLException as e:
86 # connection close failed.
87 print("error: %s"%e.getMessage())