-1
connection class:
public class ConnectionFactory {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/helpsemeq"+"?verifyServerCertificate=false&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=America/Sao_Paulo";
private static final String USER = "root";
private static final String PASS = "";
private static Connection conexao;
public Statement statement;
public ResultSet resultset;
public boolean getConnection() {
boolean result = true;
try {
Class.forName(DRIVER);
conexao = DriverManager.getConnection(URL, USER, PASS);
result = true;
} catch (ClassNotFoundException | SQLException ex) {
result = false;
throw new RuntimeException("Erro na conexão: ", ex);
}
return result;
}
public static void closeConnection(Connection con) {
try {
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void closeConnection(Connection con, PreparedStatement stmt) {
closeConnection(con);
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException ex) {
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs) {
closeConnection(con, stmt);
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ex) {
Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void executeSQL(String sql) {
try {
statement = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
resultset = statement.executeQuery(sql);
} catch (SQLException sqlex) {
System.out.println("Não foi possivel executar o comando: \n" + sqlex + "\n o sql passado foi: \n" + sql);
}
}
}
here is the method that is in the connection class to run the query:
public void executeSQL(String sql) {
try {
statement = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
resultset = statement.executeQuery(sql);
} catch (SQLException sqlex) {
System.out.println("Não foi possivel executar o comando: \n" + sqlex + "\n o sql passado foi: \n" + sql);
}
}
the other method that gets inside the jframe:
public void validaLogin(){
try {
ConnectionFactory conn = new ConnectionFactory();
conn.getConnection();
String sql = "SELECT login,senha FROM usuario where login = '" + jTextField1.getText() + "' and senha = '" + jTextField2.getText() + "'";
conn.executeSQL(sql);
//Se houver resultado, ou seja, se validar o usuario e senha, faça algo.
if (conn.resultset.next()) {
new NewJFrame1().setVisible(true);
dispose();
} else {
System.out.println("Acesso negado.");
}
}catch(SQLException e){
System.out.println("Erro: "+e);
}
}
it would be possible to do all this in just one method?
I’m sorry but I don’t quite understand your question. Is the validation you say authentication? And why only one method?
– rafaelim
Yes that, has the method in the connection class and in the class of jframe, I wanted to put in only one method, because I aim to also save the id in the query, after the login it will go to a screen where it will be possible to add or change values and then wanted to save the user id to change in the database according to the user id. ( ex has table user, has requests table, requests will receive the id_login) and then when to change or add a request after being logged in save with the logged in id_login.
– Felipe
Lega, I get it. I think it’s cool your code. Maybe, an improvement I can suggest, is to create two more classes. User and user. The User class would store id and the User class(Data Access Objetct) would provide an instance of the User class with id. If you want I can answer by showing how the classes could be.
– rafaelim
@rafaelim would be very grateful bro, and if you could point me something to study more about these interactions with comics, I’m studying s.i, but so far only gave java and had no interaction with comics :(
– Felipe