ModestGenius

ConnectionWithDB

Mar 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6.  * Created by Максим on 03.03.2016.
  7.  */
  8. public class ConnectionWithDB {
  9.     public static ResultSet connect() throws SQLException {
  10.     try {
  11.         Class.forName("org.postgresql.Driver");
  12.     } catch (ClassNotFoundException e) {
  13.         e.printStackTrace();
  14.     }
  15.     Connection connection = null;
  16.     try {
  17.         connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Authentication","postgres","postgres");
  18.     } catch (SQLException e) {
  19.         e.printStackTrace();
  20.     }
  21.     Statement st = connection.createStatement();
  22.     ResultSet rs = st.executeQuery("select * from authentication");
  23.     if (connection != null) {
  24.         try {
  25.             connection.close();
  26.             return rs;
  27.         } catch (SQLException ex) {
  28.             ex.printStackTrace();
  29.         }
  30.     } else {
  31.     }
  32.         return rs;
  33.     }
  34.  
  35.  
  36.  
  37.     public static void connect(String login, String password) throws SQLException {
  38.         try {
  39.             Class.forName("org.postgresql.Driver");
  40.         } catch (ClassNotFoundException e) {
  41.             e.printStackTrace();
  42.         }
  43.         Connection connection = null;
  44.         try {
  45.             connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Authentication","postgres","postgres");
  46.         } catch (SQLException e) {
  47.             e.printStackTrace();
  48.         }
  49.         String query = "insert into authentication values (?,?)";
  50.         PreparedStatement pStatement = connection.prepareStatement(query);
  51.             pStatement.setString(1, login);
  52.             pStatement.setString(2, password);
  53.             pStatement.executeUpdate();
  54.         if (connection != null) {
  55.             try {
  56.                 connection.close();
  57.             } catch (SQLException ex) {
  58.                 ex.printStackTrace();
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment