Skip to content

Commit f6cf5aa

Browse files
author
Denevell - Mainly a mobile developer
committed
Update tomcat7-jdbc-jdni-sqlite.md
1 parent 1d1fe42 commit f6cf5aa

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

tomcat7-jdbc-jdni-sqlite.md

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,46 @@ First download the sqlite jdbc driver from https://bitbucket.org/xerial/sqlite-j
77

88
Then add a reference to a new jdbc resource in your web/META-INF/context.xml file. It defines a JNDI name, its class type, the driver class name that relates to the jar we just installed above, and a url to connect to the database. In my case I'm pointing to a directory (which must exist) on the file system.
99

10-
<Context>
11-
<Resource name="jdbc/sqlite"
12-
type="javax.sql.DataSource"
13-
driverClassName="org.sqlite.JDBC"
14-
url="jdbc:sqlite:/var/lib/tomcat7/dbs/test.db"
15-
>
16-
</Resource>
17-
</Context>
10+
<Context>
11+
<Resource name="jdbc/sqlite"
12+
type="javax.sql.DataSource"
13+
driverClassName="org.sqlite.JDBC"
14+
url="jdbc:sqlite:/var/lib/tomcat7/dbs/test.db"
15+
/>
16+
</Context>
1817

1918
In addition, you have to add the resource in your web.xml file. Note the name is the same the name above.
2019

21-
...
22-
<resource-ref>
23-
<res-ref-name>jdbc/sqlite</res-ref-name>
24-
<res-type>javax.sql.DataSource</res-type>
25-
</resource-ref>
26-
...
20+
...
21+
<resource-ref>
22+
<res-ref-name>jdbc/sqlite</res-ref-name>
23+
<res-type>javax.sql.DataSource</res-type>
24+
</resource-ref>
25+
...
2726

2827
Now you can make reference to the database in your Java code. This is lifted from the sqlite example page:
2928

30-
Connection conn = null;
31-
try {
32-
Context ctx = new InitialContext();
33-
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sqlite");
34-
conn = ds.getConnection();
35-
Statement statement = conn.createStatement();
36-
try {
37-
statement.executeUpdate("create table thing(x integer)");
38-
} catch (Exception e) {
39-
// Could well be already there
40-
}
41-
statement.executeUpdate("insert into thing values(42)");
42-
ResultSet rs = statement.executeQuery("select * from thing");
43-
while (rs.next()) {
44-
writer.println("id = " + rs.getInt(1));
45-
46-
} catch (Exception e) {
47-
writer.println(e.getMessage());
48-
} finally {
49-
try {
50-
if (conn != null) conn.close();
51-
} catch (SQLException e) {
52-
writer.println(e);
53-
}
54-
}
29+
Connection conn = null;
30+
try {
31+
Context ctx = new InitialContext();
32+
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sqlite");
33+
conn = ds.getConnection();
34+
Statement statement = conn.createStatement();
35+
try {
36+
statement.executeUpdate("create table thing(x integer)");
37+
} catch (Exception e) {
38+
// Could well be already there
39+
}
40+
statement.executeUpdate("insert into thing values(42)");
41+
ResultSet rs = statement.executeQuery("select * from thing");
42+
while (rs.next()) {
43+
writer.println("id = " + rs.getInt(1));
44+
} catch (Exception e) {
45+
writer.println(e.getMessage());
46+
} finally {
47+
try {
48+
if (conn != null) conn.close();
49+
} catch (SQLException e) {
50+
writer.println(e);
51+
}
52+
}

0 commit comments

Comments
 (0)