Guest User

Untitled

a guest
Jan 14th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Conexion {
  7.  
  8.  
  9. Connection cnx;
  10.  
  11. public Connection getCnx(){
  12. try {
  13. Class.forName("com.mysql.jdbc.Driver");
  14. cnx=DriverManager.getConnection("jdbc:mysql://localhost:3306/myl", "root", "");
  15.  
  16. }catch (ClassNotFoundException | SQLException ex) {
  17. JOptionPane.showMessageDialog(null,"Error de Acceso:n No hay conexion a la base de datosn:"+ ex,"Error", JOptionPane.ERROR_MESSAGE);
  18.  
  19. }
  20.  
  21. return cnx;
  22. }
  23.  
  24. public void closeCnx() throws SQLException{
  25.  
  26. if(cnx != null){
  27.  
  28. cnx.close();
  29.  
  30. }
  31. }
  32.  
  33. import java.io.IOException;
  34. import java.sql.Connection;
  35. import java.sql.PreparedStatement;
  36. import java.sql.ResultSet;
  37. import java.sql.SQLException;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40. import javax.swing.table.DefaultTableModel;
  41.  
  42. public class Querys {
  43.  
  44.  
  45. Conexion CNX=new Conexion();
  46. PreparedStatement SQL;
  47. List validation = new ArrayList();
  48. Connection con = CNX.getCnx();
  49. Object[] titulos = {"id","nombre","tipo","fecha","hora"};
  50. DefaultTableModel dtm = new DefaultTableModel(null,titulos);
  51.  
  52. public List LogIn(String user) throws SQLException, IOException, ClassNotFoundException {
  53. String queryPassword = "SELECT users, pass FROM log WHERE users='"+user+"'";
  54. SQL = con.prepareStatement(queryPassword);
  55. ResultSet result = SQL.executeQuery();
  56. String PassDB = null;
  57. String UserDB = null;
  58. if (result.next()){
  59. UserDB=result.getString("users");
  60. PassDB=result.getString("pass");
  61. validation.add(UserDB);
  62. validation.add(PassDB);
  63.  
  64. }
  65. CNX.closeCnx();
  66. return validation;
  67. }
  68.  
  69. public DefaultTableModel buscarTorneo(String fecha) throws SQLException, IOException, ClassNotFoundException {
  70.  
  71. String queryPassword = "SELECT * FROM `torneo` WHERE fecha='"+fecha+"'";
  72. SQL = con.prepareStatement(queryPassword);
  73. ResultSet result = SQL.executeQuery();
  74. if (result.next()){
  75.  
  76. dtm.addRow(new Object[]{
  77. result.getString("id"),
  78. result.getString("nombre"),
  79. result.getString("tipo"),
  80. result.getString("fecha"),
  81. result.getString("hora")
  82. });
  83. }
  84. CNX.closeCnx();
  85. return dtm;
  86. }
  87.  
  88. public void crearTorneo(String nombre,String tipo, String fecha,String h) throws SQLException, IOException, ClassNotFoundException{
  89. String query = "INSERT INTO torneo (nombre, tipo, fecha, hora) "
  90. + "VALUES ('"+nombre+"','"+tipo+"','"+fecha+"','"+h+"');";
  91. SQL = con.prepareStatement(query);
  92. SQL.execute();
  93. CNX.closeCnx();
  94. }
Add Comment
Please, Sign In to add comment