Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author
- */
- public class PostgreSQLDatabase {
- public static void connect() {
- System.out.println("-------- PostgreSQL " + "JDBC Connection Testing ------------");
- try {
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
- e.printStackTrace();
- return;
- }
- System.out.println("PostgreSQL JDBC Driver Registered!");
- }
- public static void connectionPostgreSQL(){
- connect();
- int count = 0;
- String login = "baza";
- String passwd = "Wydrych";
- Connection connection = null;
- String url = "jdbc:postgresql://localhost:5432/bomberman";
- String username = "postgres";
- String password = "bela12";
- PreparedStatement zapytaniePostgreSQL = null;
- try {
- connection = DriverManager.getConnection(url, username, password);
- Statement stmt = connection.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT count(*) as count FROM players WHERE login = '"+login+"'");
- while(rs.next()){
- count = rs.getInt("count");
- }
- if(count == 0){
- zapytaniePostgreSQL = connection.prepareStatement("INSERT INTO players (login, password) VALUES (?,?)");
- zapytaniePostgreSQL.setString(1, login);
- zapytaniePostgreSQL.setString(2, passwd);
- zapytaniePostgreSQL.executeUpdate();
- }
- else{
- System.out.println("Podane konto istnieje!");
- }
- } catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- return;
- }
- if (connection != null) {
- System.out.println("You made it, take control your database now!");
- } else {
- System.out.println("Failed to make connection!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement