Skip to content

Commit 16ba9c8

Browse files
Merge pull request #2 from KHU1399WinterAP/master
database added
2 parents fb11980 + 89a7495 commit 16ba9c8

File tree

9 files changed

+321
-5
lines changed

9 files changed

+321
-5
lines changed

.idea/workspace.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
package config;
3+
4+
public class databaseConfig {
5+
public static final String CONNECTION_URL = "jdbc:mysql://localhost:3306/seabattle";
6+
public static final String USERNAME = "root";
7+
public static final String PASSWORD = "perchalaghq.127.atel";
8+
}

src/main/java/gui/Dashboard.form

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
4+
<Properties>
5+
<Property name="defaultCloseOperation" type="int" value="3"/>
6+
<Property name="resizable" type="boolean" value="false"/>
7+
</Properties>
8+
<SyntheticProperties>
9+
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
10+
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
11+
</SyntheticProperties>
12+
<AuxValues>
13+
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
14+
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
15+
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
16+
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
17+
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
18+
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
19+
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
20+
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
21+
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
22+
</AuxValues>
23+
24+
<Layout>
25+
<DimensionLayout dim="0">
26+
<Group type="103" groupAlignment="0" attributes="0">
27+
<Group type="102" attributes="0">
28+
<Group type="103" groupAlignment="0" attributes="0">
29+
<Group type="102" attributes="0">
30+
<EmptySpace min="-2" pref="168" max="-2" attributes="0"/>
31+
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
32+
</Group>
33+
<Group type="102" alignment="0" attributes="0">
34+
<EmptySpace min="-2" pref="136" max="-2" attributes="0"/>
35+
<Component id="MainMenuButton" min="-2" max="-2" attributes="0"/>
36+
</Group>
37+
</Group>
38+
<EmptySpace pref="151" max="32767" attributes="0"/>
39+
</Group>
40+
</Group>
41+
</DimensionLayout>
42+
<DimensionLayout dim="1">
43+
<Group type="103" groupAlignment="0" attributes="0">
44+
<Group type="102" alignment="0" attributes="0">
45+
<EmptySpace min="-2" pref="120" max="-2" attributes="0"/>
46+
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
47+
<EmptySpace pref="81" max="32767" attributes="0"/>
48+
<Component id="MainMenuButton" min="-2" max="-2" attributes="0"/>
49+
<EmptySpace min="-2" pref="62" max="-2" attributes="0"/>
50+
</Group>
51+
</Group>
52+
</DimensionLayout>
53+
</Layout>
54+
<SubComponents>
55+
<Component class="javax.swing.JLabel" name="jLabel1">
56+
<Properties>
57+
<Property name="text" type="java.lang.String" value="Hello Friend"/>
58+
</Properties>
59+
</Component>
60+
<Component class="javax.swing.JButton" name="MainMenuButton">
61+
<Properties>
62+
<Property name="text" type="java.lang.String" value="Go to main menu"/>
63+
</Properties>
64+
<Events>
65+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="MainMenuButtonActionPerformed"/>
66+
</Events>
67+
</Component>
68+
</SubComponents>
69+
</Form>

src/main/java/gui/Dashboard.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package gui;
7+
8+
/**
9+
*
10+
* @author arastoo
11+
*/
12+
public class Dashboard extends javax.swing.JFrame {
13+
14+
/**
15+
* Creates new form Dashboard
16+
*/
17+
public Dashboard() {
18+
initComponents();
19+
}
20+
21+
/**
22+
* This method is called from within the constructor to initialize the form.
23+
* WARNING: Do NOT modify this code. The content of this method is always
24+
* regenerated by the Form Editor.
25+
*/
26+
@SuppressWarnings("unchecked")
27+
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
28+
private void initComponents() {
29+
30+
jLabel1 = new javax.swing.JLabel();
31+
MainMenuButton = new javax.swing.JButton();
32+
33+
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
34+
setResizable(false);
35+
36+
jLabel1.setText("Hello Friend");
37+
38+
MainMenuButton.setText("Go to main menu");
39+
MainMenuButton.addActionListener(new java.awt.event.ActionListener() {
40+
public void actionPerformed(java.awt.event.ActionEvent evt) {
41+
MainMenuButtonActionPerformed(evt);
42+
}
43+
});
44+
45+
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
46+
getContentPane().setLayout(layout);
47+
layout.setHorizontalGroup(
48+
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
49+
.addGroup(layout.createSequentialGroup()
50+
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
51+
.addGroup(layout.createSequentialGroup()
52+
.addGap(168, 168, 168)
53+
.addComponent(jLabel1))
54+
.addGroup(layout.createSequentialGroup()
55+
.addGap(136, 136, 136)
56+
.addComponent(MainMenuButton)))
57+
.addContainerGap(151, Short.MAX_VALUE))
58+
);
59+
layout.setVerticalGroup(
60+
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
61+
.addGroup(layout.createSequentialGroup()
62+
.addGap(120, 120, 120)
63+
.addComponent(jLabel1)
64+
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
65+
.addComponent(MainMenuButton)
66+
.addGap(62, 62, 62))
67+
);
68+
69+
pack();
70+
}// </editor-fold>//GEN-END:initComponents
71+
72+
private void MainMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MainMenuButtonActionPerformed
73+
this.dispose();
74+
MainMenu mainmenu = new MainMenu();
75+
mainmenu.setVisible(true);
76+
}//GEN-LAST:event_MainMenuButtonActionPerformed
77+
78+
/**
79+
* @param args the command line arguments
80+
*/
81+
public static void main(String args[]) {
82+
/* Set the Nimbus look and feel */
83+
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
84+
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
85+
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
86+
*/
87+
try {
88+
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
89+
if ("Nimbus".equals(info.getName())) {
90+
javax.swing.UIManager.setLookAndFeel(info.getClassName());
91+
break;
92+
}
93+
}
94+
} catch (ClassNotFoundException ex) {
95+
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
96+
} catch (InstantiationException ex) {
97+
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
98+
} catch (IllegalAccessException ex) {
99+
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
100+
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
101+
java.util.logging.Logger.getLogger(Dashboard.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
102+
}
103+
//</editor-fold>
104+
105+
/* Create and display the form */
106+
java.awt.EventQueue.invokeLater(new Runnable() {
107+
public void run() {
108+
new Dashboard().setVisible(true);
109+
}
110+
});
111+
}
112+
113+
// Variables declaration - do not modify//GEN-BEGIN:variables
114+
private javax.swing.JButton MainMenuButton;
115+
private javax.swing.JLabel jLabel1;
116+
// End of variables declaration//GEN-END:variables
117+
}

src/main/java/gui/Database.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package gui;
2+
3+
4+
import config.databaseConfig;
5+
import java.sql.Statement;
6+
import java.sql.Connection;
7+
import java.sql.DriverManager;
8+
import java.sql.PreparedStatement;
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
import java.util.ArrayList;
12+
import models.User;
13+
14+
15+
public class Database {
16+
public static Connection connection = null;
17+
public Database() throws SQLException{
18+
try{
19+
connection = DriverManager.getConnection(databaseConfig.CONNECTION_URL, databaseConfig.USERNAME, databaseConfig.PASSWORD);
20+
}
21+
catch (SQLException throwables) {
22+
throwables.printStackTrace();
23+
}
24+
}
25+
26+
public static ArrayList<User> sellectAllUsers(){
27+
try{
28+
var users = new ArrayList<User>();
29+
Statement statement = (Statement) connection.createStatement();
30+
ResultSet resultSet = statement.executeQuery("SELECT * FROM user");
31+
32+
while(resultSet.next())
33+
{
34+
User user = new User(resultSet.getString("username"), resultSet.getString("password"));
35+
users.add(user);
36+
}
37+
38+
return users;
39+
}
40+
catch (SQLException throwables)
41+
{
42+
throwables.printStackTrace();
43+
return new ArrayList<>();
44+
}
45+
46+
47+
}
48+
49+
public static void insertIntoUser(User user) {
50+
try {
51+
PreparedStatement statement = connection.prepareStatement("INSERT INTO user VALUES(?, ?)");
52+
statement.setString(1, user.Username);
53+
statement.setString(2, user.Password);
54+
55+
statement.executeUpdate();
56+
} catch (SQLException throwables) {
57+
throwables.printStackTrace();
58+
}
59+
}
60+
61+
public static User getUserByUsername(String username) {
62+
try {
63+
PreparedStatement statement = connection.prepareStatement("SELECT * FROM user WHERE username = ?");
64+
statement.setString(1, username);
65+
66+
ResultSet resultSet = statement.executeQuery();
67+
68+
if (resultSet.next())
69+
return new User(resultSet.getString("username"), resultSet.getString("password"));
70+
} catch (SQLException throwables) {
71+
throwables.printStackTrace();
72+
}
73+
74+
return null;
75+
}
76+
}

src/main/java/gui/LoginMenu.form

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@
200200
</Property>
201201
<Property name="text" type="java.lang.String" value="Login"/>
202202
</Properties>
203+
<Events>
204+
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="LoginButtonActionPerformed"/>
205+
</Events>
203206
</Component>
204207
<Component class="javax.swing.JButton" name="MainMenuButton">
205208
<Properties>

src/main/java/gui/LoginMenu.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@
55
*/
66
package gui;
77

8+
import javax.swing.JFrame;
9+
import models.User;
10+
811
/**
912
*
1013
* @author arastoo
1114
*/
1215
public class LoginMenu extends javax.swing.JFrame {
13-
16+
17+
private JFrame previousFrame;
1418
/**
1519
* Creates new form LoginMenu
1620
*/
21+
public LoginMenu(JFrame previousFrame) {
22+
this.previousFrame = previousFrame;
23+
initComponents();
24+
25+
26+
27+
}
1728
public LoginMenu() {
1829
initComponents();
1930
}
@@ -90,6 +101,11 @@ private void initComponents() {
90101

91102
LoginButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
92103
LoginButton.setText("Login");
104+
LoginButton.addActionListener(new java.awt.event.ActionListener() {
105+
public void actionPerformed(java.awt.event.ActionEvent evt) {
106+
LoginButtonActionPerformed(evt);
107+
}
108+
});
93109

94110
MainMenuButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
95111
MainMenuButton.setText("Go to Main Menu");
@@ -159,9 +175,27 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
159175
}// </editor-fold>//GEN-END:initComponents
160176

161177
private void MainMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MainMenuButtonActionPerformed
162-
178+
this.dispose();
179+
previousFrame.setVisible(true);
163180
}//GEN-LAST:event_MainMenuButtonActionPerformed
164181

182+
private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginButtonActionPerformed
183+
184+
185+
String username = UserNameField.getText();
186+
String password = String.valueOf(PasswordField.getPassword());
187+
188+
if (!username.isBlank()) {
189+
User user = Database.getUserByUsername(username);
190+
191+
if (user != null && user.Password.equals(password)) {
192+
Dashboard dashboard = new Dashboard();
193+
dashboard.setVisible(true);
194+
195+
this.dispose();
196+
previousFrame.setVisible(true);
197+
}//GEN-LAST:event_LoginButtonActionPerformed
198+
165199
/**
166200
* @param args the command line arguments
167201
*/

src/main/java/gui/RegisterMenu.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package gui;
77

8+
89
import java.awt.event.WindowEvent;
910
import java.awt.event.WindowListener;
1011
import javax.swing.JFrame;
@@ -180,6 +181,7 @@ private void RegisterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GE
180181
User user = new User(UserNameField.getText(),PasswordField.getPassword());
181182
System.out.println(user);
182183

184+
Database.insertIntoUser(user);
183185
this.dispose();
184186
previousFrame.setVisible(true);
185187
}//GEN-LAST:event_RegisterButtonActionPerformed

src/main/java/models/User.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ public class User {
55
public String Username;
66
public String Password;
77

8-
public User(String Username, char[] Password) {
8+
public User(String Username, String Password) {
99
this.Username = Username;
10-
this.Password = String.valueOf(Password);
10+
this.Password = Password;
11+
}
12+
13+
public User(String Username, char[] Password) {
14+
this(Username, String.valueOf(Password));
1115
}
1216

1317
@Override

0 commit comments

Comments
 (0)