Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. import org.junit.Test;
  9.  
  10. public class PureJDBCConnectionTest {
  11.  
  12.     @Test
  13.     public void test() throws Exception {
  14.  
  15.         Connection connection = connect();
  16.  
  17. //      this is okay
  18. //      PreparedStatement stmt = connection.prepareStatement("SELECT COUNT(*) FROM tempalarms WHERE ack is true");
  19. //      ResultSet rs = stmt.executeQuery();
  20.  
  21.  
  22.         PreparedStatement stmt = connection.prepareStatement("SELECT COUNT(*) FROM tempalarms WHERE ack is ?");
  23.         stmt.setBoolean(1, true);
  24.         ResultSet rs = stmt.executeQuery();
  25.  
  26.         // this has error
  27.  
  28.         while (rs.next()) {
  29.             System.out.println(rs.getInt(0));
  30.         }
  31.  
  32.         rs.close();
  33.  
  34.         stmt.close();
  35.  
  36.         connection.close();
  37.     }
  38.  
  39.     public static Connection connect() {
  40.         try {
  41.             Class.forName("org.postgresql.Driver");
  42.         } catch (ClassNotFoundException e1) {
  43.             // TODO Auto-generated catch block
  44.             e1.printStackTrace();
  45.         }
  46.         Properties properties = new Properties();
  47.         properties.setProperty("user", "user");
  48.         properties.setProperty("password", "pass");
  49.         try {
  50.             return DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", properties);
  51.         } catch (SQLException e) {
  52.             e.printStackTrace();
  53.         }
  54.         return null;
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement