Java examples for java.sql:h2
load All User Names from h2 database
//package com.java2s; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class Main { private static final String DB_CONN_URL = "jdbc:h2:file:~/h2db"; private static final String USERNAME = "surya"; private static final String PASSWORD = "kiran"; public static List<String> loadAllUserNames() { List<String> userNames = new ArrayList<String>(); String selectStmt = "select name from users order by name"; try (Connection conn = getConnection(); PreparedStatement stmt = conn.prepareStatement(selectStmt); ResultSet rs = stmt.executeQuery();) { while (rs.next()) { userNames.add(rs.getString("name")); }/* w ww. ja v a2s . c om*/ } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return userNames; } public static Connection getConnection() throws SQLException, ClassNotFoundException { Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection(DB_CONN_URL, USERNAME, PASSWORD); return conn; } }