Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. package game;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. /*
  11. * To change this license header, choose License Headers in Project Properties.
  12. * To change this template file, choose Tools | Templates
  13. * and open the template in the editor.
  14. */
  15. /**
  16. *
  17. * @author
  18. */
  19. public class PostgreSQLDatabase {
  20. public static void connect() {
  21. System.out.println("-------- PostgreSQL " + "JDBC Connection Testing ------------");
  22. try {
  23. Class.forName("org.postgresql.Driver");
  24. } catch (ClassNotFoundException e) {
  25. System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
  26. e.printStackTrace();
  27. return;
  28. }
  29. System.out.println("PostgreSQL JDBC Driver Registered!");
  30. }
  31. public static void connectionPostgreSQL(){
  32.  
  33. connect();
  34. int count = 0;
  35. String login = "baza";
  36. String passwd = "Wydrych";
  37. Connection connection = null;
  38. String url = "jdbc:postgresql://localhost:5432/bomberman";
  39. String username = "postgres";
  40. String password = "bela12";
  41. PreparedStatement zapytaniePostgreSQL = null;
  42.  
  43. try {
  44. connection = DriverManager.getConnection(url, username, password);
  45. Statement stmt = connection.createStatement();
  46. ResultSet rs = stmt.executeQuery("SELECT count(*) as count FROM players WHERE login = '"+login+"'");
  47. while(rs.next()){
  48. count = rs.getInt("count");
  49. }
  50. if(count == 0){
  51. zapytaniePostgreSQL = connection.prepareStatement("INSERT INTO players (login, password) VALUES (?,?)");
  52. zapytaniePostgreSQL.setString(1, login);
  53. zapytaniePostgreSQL.setString(2, passwd);
  54. zapytaniePostgreSQL.executeUpdate();
  55. }
  56. else{
  57. System.out.println("Podane konto istnieje!");
  58. }
  59.  
  60.  
  61.  
  62. } catch (SQLException e) {
  63. System.out.println("Connection Failed! Check output console");
  64. e.printStackTrace();
  65. return;
  66. }
  67. if (connection != null) {
  68. System.out.println("You made it, take control your database now!");
  69. } else {
  70. System.out.println("Failed to make connection!");
  71. }
  72. }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement