Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Properties;
- import org.junit.Test;
- public class PureJDBCConnectionTest {
- @Test
- public void test() throws Exception {
- Connection connection = connect();
- // this is okay
- // PreparedStatement stmt = connection.prepareStatement("SELECT COUNT(*) FROM tempalarms WHERE ack is true");
- // ResultSet rs = stmt.executeQuery();
- PreparedStatement stmt = connection.prepareStatement("SELECT COUNT(*) FROM tempalarms WHERE ack is ?");
- stmt.setBoolean(1, true);
- ResultSet rs = stmt.executeQuery();
- // this has error
- while (rs.next()) {
- System.out.println(rs.getInt(0));
- }
- rs.close();
- stmt.close();
- connection.close();
- }
- public static Connection connect() {
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- Properties properties = new Properties();
- properties.setProperty("user", "user");
- properties.setProperty("password", "pass");
- try {
- return DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", properties);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement