Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package BaseDeDatos;
  2.  
  3. import java.awt.Color;
  4. import java.sql.*;
  5.  
  6. import Clases.Configuracion;
  7. import Clases.Jugador;
  8.  
  9. public class BaseDeDatos {
  10. private static final String URL = "jdbc:postgresql://ec2-79-125-13-42.eu-west-1.compute.amazonaws.com:5432/ddq7vec664eksu?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";
  11. private static final String USERNAME = "whdcyqdctwfnjx";
  12. private static final String PASSWORD = "f468c95bedacc40a787b1d6fafe27a91bbc45309328b21d217003ab58a5930a7";
  13.  
  14. public static Connection initBD( ) {
  15. try {
  16. Class.forName("org.postgresql.Driver");
  17. Connection con = DriverManager.getConnection(URL, USERNAME, PASSWORD );
  18. return con;
  19. } catch (Exception e) {
  20.  
  21. e.printStackTrace();
  22. return null;
  23. }
  24. }
  25.  
  26.  
  27. //Obtener statement
  28.  
  29. public static Statement ObtenerStatement(Connection con){
  30. Statement st = null;;
  31. try {
  32. st = con.createStatement();
  33. } catch (SQLException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. return st;
  38. }
  39. public static Statement usarCrearTablasBD( Connection con ) {
  40. try {
  41. Statement statement = con.createStatement();
  42. statement.setQueryTimeout(30); // poner timeout 30 msg
  43. try {
  44. statement.executeUpdate("create table usuarios " +
  45. "(nickname text not null primary key, Colornave integer, Configuracion text, MaxPunt1 Integer, MaxPunt2 integer, MaxPunt3 Integer)");
  46. System.out.println("TABLA CREADA");
  47. } catch (SQLException e) {
  48. } // Tabla ya existe. Nada que hacer
  49.  
  50. return statement;
  51. } catch (SQLException e) {
  52.  
  53. return null;
  54. }
  55. }
  56.  
  57. public static boolean usuarioInsert( Statement st,String Nickname) {
  58. boolean correcto= true;
  59.  
  60. String sentSQL = "";
  61.  
  62.  
  63. sentSQL = "Insert into usuarios values(" +
  64. "'" + Nickname+"',0,'WASD',0,0,0)";
  65.  
  66. try {
  67. st.executeUpdate(sentSQL);
  68. } catch (SQLException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. correcto=false;
  72. }
  73.  
  74. return correcto;
  75.  
  76. }
  77. public static boolean CambiarConfiguracion( Statement st, Jugador u,int TipoConfiguracion ) {
  78. boolean completado = true;
  79. String sentSQL = "update usuarios set" +
  80. " Configuracion=" + TipoConfiguracion +
  81. " where nick='" + u.getNombre() + "'";
  82. try{
  83. st.executeUpdate(sentSQL);
  84.  
  85.  
  86.  
  87.  
  88. } catch(Exception ex){
  89. completado = false;
  90. }
  91. return completado;
  92.  
  93.  
  94. }
  95. public static boolean CambiarColorNave( Statement st, Jugador u,int ColorNave ) {
  96. boolean completado = true;
  97. String sentSQL = "update usuarios set" +
  98. " Configuracion=" + ColorNave +
  99. " where nick='" + u.getNombre() + "'";
  100. try{
  101. st.executeUpdate(sentSQL);
  102.  
  103.  
  104.  
  105.  
  106. } catch(Exception ex){
  107. completado = false;
  108. }
  109. return completado;
  110.  
  111.  
  112. }
  113. public static boolean ActualizarPuntuacion( Statement st, Jugador u, int puntuacion, int numerodepuntuacion ) {
  114.  
  115. boolean completado = true;
  116. String sentSQL = "update usuarios set" +
  117. " MaxPunt" + numerodepuntuacion + " =" + puntuacion +
  118. " where nick='" + u.getNombre() + "'";
  119. try{
  120. st.executeUpdate(sentSQL);
  121.  
  122.  
  123.  
  124.  
  125. } catch(Exception ex){
  126. completado = false;
  127. }
  128. return completado;
  129.  
  130.  
  131. }
  132. public static ResultSet ObtenerDatosUsuario(String nickname, Statement st){
  133. ResultSet rs = null;
  134. String sentSql = "Select * from usuarios where nickname = '" + nickname + "'";
  135.  
  136. try {
  137. rs = st.executeQuery(sentSql);
  138. } catch (SQLException e) {
  139. // TODO Auto-generated catch block
  140. e.printStackTrace();
  141. }
  142. return rs;
  143.  
  144.  
  145.  
  146.  
  147.  
  148. }
  149.  
  150. public static Jugador CargarJugador(ResultSet rs){
  151. Jugador e = new Jugador();
  152. try{
  153. System.out.println("Hola");
  154. System.out.println("PENE");
  155. e.setNombre(rs.getString("nickname"));
  156.  
  157. e.setMaxPunt1(rs.getInt("MaxPunt1"));
  158. e.setMaxPunt2(rs.getInt("MaxPunt2"));
  159. e.setMaxPunt3(rs.getInt("MaxPunt3"));
  160. Configuracion confi = new Configuracion();
  161. confi.setTeclas(rs.getString("Configuracion"));
  162. int color = rs.getInt("ColorNave");
  163. if (color ==0){
  164. confi.setColorNave(Color.BLUE);
  165.  
  166. }
  167. if (color ==1){
  168. confi.setColorNave(Color.RED);
  169.  
  170. }
  171. if (color ==2){
  172. confi.setColorNave(Color.GREEN);
  173. }
  174. e.setConfi(confi);
  175.  
  176.  
  177. } catch (Exception ex){
  178.  
  179. }
  180. return e;
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement