1
2
3
4
5
6
7
8
9
10
11
12package jdbc_wrapper;
13
14import java.sql.Connection;
15import java.sql.PreparedStatement;
16import java.sql.ResultSet;
17import java.sql.SQLException;
18import java.sql.Statement;
19import java.util.logging.ConsoleHandler;
20import java.util.logging.Level;
21import java.util.logging.Logger;
22
23public class Main
24{
25 public static void main(String[] args) throws Exception
26 {
27 configureLogging();
28 Connection connection = createConnection();
29 setupTable(connection);
30 queryTable(connection);
31 }
32
33 private static void configureLogging()
34 {
35 ConsoleHandler handler = new ConsoleHandler();
36 Logger logger = Logger.getLogger("jdbc.trace");
37
38 handler.setLevel(Level.FINER);
39 logger.setLevel(Level.FINER);
40 logger.addHandler(handler);
41 }
42
43 private static Connection createConnection() throws ClassNotFoundException, SQLException
44 {
45
46 Class.forName("org.h2.Driver");
47 Connection connection = ConnectionManager.getConnection("jdbc:h2:mem:test", "sa", "");
48 return connection;
49 }
50
51 private static void queryTable(Connection connection) throws SQLException
52 {
53 PreparedStatement statement = connection.prepareStatement("SELECT * FROM test");
54 ResultSet results = statement.executeQuery();
55 while (results.next())
56 {
57 int columnValue = results.getInt(1);
58 System.out.println("Column value: " + columnValue);
59 }
60 }
61
62 private static void setupTable(Connection connection) throws SQLException
63 {
64 Statement statement = connection.createStatement();
65 statement.execute("CREATE TABLE test ( value INTEGER NOT NULL )");
66 statement.execute("INSERT INTO test VALUES ( 1 )");
67 statement.execute("INSERT INTO test VALUES ( 2 )");
68 }
69
70}
// Copyright 2007, Tim Vernum
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package jdbc_wrapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main
{
public static void main(String[] args) throws Exception
{
configureLogging();
Connection connection = createConnection();
setupTable(connection);
queryTable(connection);
}
private static void configureLogging()
{
ConsoleHandler handler = new ConsoleHandler();
Logger logger = Logger.getLogger("jdbc.trace");
handler.setLevel(Level.FINER);
logger.setLevel(Level.FINER);
logger.addHandler(handler);
}
private static Connection createConnection() throws ClassNotFoundException, SQLException
{
// This sample is configured to use the H2 database (http://www.h2database.com), but other databases can be used in its place
Class.forName("org.h2.Driver");
Connection connection = ConnectionManager.getConnection("jdbc:h2:mem:test", "sa", "");
return connection;
}
private static void queryTable(Connection connection) throws SQLException
{
PreparedStatement statement = connection.prepareStatement("SELECT * FROM test");
ResultSet results = statement.executeQuery();
while (results.next())
{
int columnValue = results.getInt(1);
System.out.println("Column value: " + columnValue);
}
}
private static void setupTable(Connection connection) throws SQLException
{
Statement statement = connection.createStatement();
statement.execute("CREATE TABLE test ( value INTEGER NOT NULL )");
statement.execute("INSERT INTO test VALUES ( 1 )");
statement.execute("INSERT INTO test VALUES ( 2 )");
}
}