Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01b7c0b

Browse files
Using Hibernate to consult the DB and validate the username and password.
1 parent ddcbc5c commit 01b7c0b

File tree

1 file changed

+19
-46
lines changed

1 file changed

+19
-46
lines changed

‎src/Login.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11

22
import java.awt.Image;
33
import java.awt.Toolkit;
4-
import java.sql.Connection;
5-
import java.sql.DriverManager;
6-
import java.sql.ResultSet;
7-
import java.sql.SQLException;
8-
import java.sql.Statement;
94
import java.util.logging.Level;
105
import java.util.logging.Logger;
116
import javax.swing.ImageIcon;
127
import javax.swing.JOptionPane;
8+
import org.hibernate.Session;
9+
import org.hibernate.SessionFactory;
10+
import org.hibernate.cfg.Configuration;
1311

1412

1513
public class Login extends javax.swing.JFrame {
16-
17-
/**
18-
* Creates new form Login
19-
*/
14+
2015
public Login() {
2116
initComponents();
2217

@@ -28,18 +23,8 @@ public Login() {
2823
this.Logo.setIcon(i);
2924

3025
// Establishing connection
31-
try{
32-
System.out.println("Connecting to database...");
33-
Class.forName("com.mysql.jdbc.Driver");
34-
this.connect = DriverManager.getConnection("jdbc:mysql://localhost/login", "root", "");
35-
this.statement = connect.createStatement();
36-
System.out.println("Connection established!");
37-
38-
} catch (SQLException e) {
39-
e.printStackTrace();
40-
} catch (ClassNotFoundException e) {
41-
e.printStackTrace();
42-
}
26+
this.factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(User.class).buildSessionFactory();
27+
4328
}
4429

4530
/**
@@ -140,39 +125,27 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
140125
}// </editor-fold>//GEN-END:initComponents
141126

142127
private void bt_loginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_loginActionPerformed
143-
String user = this.tf_user.getText();
128+
String username = this.tf_user.getText();
144129
String password = this.pf_password.getText();
145-
String userDB = null;
146-
String passwordDB = null;
147-
String query = "Select username, password from login";
148-
try{
149-
ResultSet resultSet = this.statement.executeQuery(query);
150-
151-
while (resultSet.next()) {
152-
// Now we can fetch the data by column name, save and use them!
153-
userDB = resultSet.getString("username");
154-
passwordDB = resultSet.getString("password");
155-
}
156-
} catch (SQLException e) {
157-
e.printStackTrace();
158-
}
159-
if(user.equals(userDB) && password.equals(passwordDB)){
130+
131+
Session session = this.factory.getCurrentSession();
132+
session.beginTransaction();
133+
134+
User user = session.get(User.class, username);
135+
136+
if(username.equals(user.getUsername()) && password.equals(user.getPassword())){
160137
JOptionPane.showMessageDialog(null, "Connected", "Status", JOptionPane.INFORMATION_MESSAGE);
161138
//System.out.println("Connected");
162-
}else{
139+
}else{
163140
JOptionPane.showMessageDialog(null, "Not connected", "Status", JOptionPane.WARNING_MESSAGE);
164141
//System.out.println("Not connected!");
165142
}
143+
session.getTransaction().commit();
166144
}//GEN-LAST:event_bt_loginActionPerformed
167145

168146
private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
169147
System.out.println("Connection will be ended!");
170-
try {
171-
this.connect.close();
172-
this.statement.close();
173-
} catch (SQLException ex) {
174-
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
175-
}
148+
this.factory.close();
176149
System.out.println("Connection ended!");
177150
}//GEN-LAST:event_formWindowClosing
178151

@@ -192,6 +165,6 @@ private void bt_addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:
192165
private javax.swing.JPasswordField pf_password;
193166
private javax.swing.JTextField tf_user;
194167
// End of variables declaration//GEN-END:variables
195-
Connectionconnect;
196-
Statementstatement = null;
168+
169+
SessionFactoryfactory;
197170
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /