Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.*;
- /**
- * Created by Максим on 03.03.2016.
- */
- public class ConnectionWithDB {
- public static ResultSet connect() throws SQLException {
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- Connection connection = null;
- try {
- connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Authentication","postgres","postgres");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery("select * from authentication");
- if (connection != null) {
- try {
- connection.close();
- return rs;
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- } else {
- }
- return rs;
- }
- public static void connect(String login, String password) throws SQLException {
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- Connection connection = null;
- try {
- connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Authentication","postgres","postgres");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- String query = "insert into authentication values (?,?)";
- PreparedStatement pStatement = connection.prepareStatement(query);
- pStatement.setString(1, login);
- pStatement.setString(2, password);
- pStatement.executeUpdate();
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment