Skip to content

Commit f6ab34f

Browse files
authored
Add files via upload
1 parent 9071a2f commit f6ab34f

5 files changed

Lines changed: 296 additions & 0 deletions

File tree

AppBackend.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import java.sql.PreparedStatement;
2+
import java.sql.Connection;
3+
import java.sql.DriverManager;
4+
import java.sql.Statement;
5+
import java.sql.SQLException;
6+
import java.io.File;
7+
import java.time.LocalDate;
8+
import java.time.LocalDateTime;
9+
import java.time.format.DateTimeFormatter;
10+
import java.util.UUID;
11+
12+
13+
public class AppBackend {
14+
protected void setupDatabase() {
15+
String appDataPath = System.getenv("APPDATA") + "\\SessionTracker";
16+
String dbPath = appDataPath + "\\user_sessions.db"; // Path to SQLite database
17+
String dbUrl = "jdbc:sqlite:" + dbPath;
18+
19+
try {
20+
// Create the directory for the database if it doesn't exist
21+
File dbDir = new File(appDataPath);
22+
boolean dirsCreated = dbDir.mkdirs(); // Returns true if the directories were created
23+
24+
if (dirsCreated) {
25+
System.out.println("Directories were successfully created.");
26+
} else {
27+
System.out.println("Directories already exist or could not be created.");
28+
}
29+
30+
// Connect to the database
31+
try (Connection conn = DriverManager.getConnection(dbUrl)) {
32+
if (conn != null) {
33+
try (Statement stmt = conn.createStatement()) {
34+
// Creates the sessions table if it doesn't already exist
35+
String createTableSQL = """
36+
CREATE TABLE IF NOT EXISTS sessions (
37+
name TEXT NOT NULL,
38+
usn TEXT NOT NULL,
39+
login_time TEXT NOT NULL,
40+
logout_time TEXT,
41+
sem TEXT,
42+
dept TEXT,
43+
batch TEXT,
44+
session_id TEXT primary key
45+
);
46+
""";
47+
stmt.execute(createTableSQL);
48+
}
49+
}
50+
}
51+
} catch (SQLException e) {
52+
System.out.println("SQLException: " + e.getMessage());
53+
System.out.println("SQLState: " + e.getSQLState());
54+
System.out.println("VendorError: " + e.getErrorCode());
55+
}
56+
}
57+
58+
59+
protected void insertData(String name,String usn) {
60+
String loginTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
61+
String dbUrl = "jdbc:sqlite:"+System.getenv("APPDATA")+"\\SessionTracker"+"\\user_sessions.db";
62+
String sessionId = UUID.randomUUID().toString();
63+
String insertSQL = """
64+
INSERT INTO sessions (name, usn, login_time, sem, dept, batch, session_id)
65+
VALUES (?, ?, ?, ?, ?, ?, ?);
66+
""";
67+
68+
try (Connection conn = DriverManager.getConnection(dbUrl);
69+
PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
70+
pstmt.setString(1, name);
71+
pstmt.setString(2, usn);
72+
pstmt.setString(3, loginTime);
73+
int year = LocalDate.now().getYear()%2000 - Integer.parseInt(usn.substring(3, 5)) + 1;
74+
int sem = LocalDate.now().getMonthValue() < 6 ? year*2-2/*even sem*/ : year*2 -1/*odd*/ ;
75+
int usnNumber = Integer.parseInt(usn.substring(7));
76+
String batch = (usnNumber < 30 || (usnNumber>60 && usnNumber<90)) ? "I" : "II" ;
77+
System.out.println(batch);
78+
pstmt.setInt(4, sem);
79+
if (usn.contains("IS")) pstmt.setString(5, "ISE");
80+
else if(usn.contains("CS")) pstmt.setString(5, "CSE");
81+
else if(usn.contains("AI")) pstmt.setString(5, "AIML");
82+
else if(usn.contains("CD")) pstmt.setString(5, "DS");
83+
else if(usn.contains("EC")) pstmt.setString(5, "ECE");
84+
else if(usn.contains("ME")) pstmt.setString(5, "MECH");
85+
else if (usn.contains("CV")) pstmt.setString(5, "CIVIL");
86+
else pstmt.setString(5, "Invalid");
87+
pstmt.setString(6, batch);
88+
pstmt.setString(7, sessionId);
89+
90+
pstmt.executeUpdate();
91+
92+
93+
94+
95+
} catch (SQLException e) {
96+
System.out.println("SQLException: " + e.getMessage());
97+
System.out.println("SQLState: " + e.getSQLState());
98+
System.out.println("VendorError: " + e.getErrorCode());
99+
}
100+
}
101+
}

CloudDatabaseUpload.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.sql.*;
2+
3+
public class CloudDatabaseUpload {
4+
5+
protected static void insertDataRemote(String name, String usn, String login_time, String logout_time, String sem, String dept, String batch, String sub, String sessionId) {
6+
String JDBC_URL = "your_DB_URL_goes_in_here";
7+
try (Connection con = DriverManager.getConnection(JDBC_URL)) {
8+
String insertData = " INSERT INTO sessions (name, usn, login_time, logout_time, sem, dept, sub, batch, session_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); ";
9+
10+
try(PreparedStatement ps = con.prepareStatement(insertData)) {
11+
ps.setString(1, name);
12+
ps.setString(2, usn);
13+
ps.setString(3, login_time);
14+
ps.setString(4, logout_time);
15+
ps.setString(5, sem);
16+
ps.setString(6, dept);
17+
ps.setString(7, sub);
18+
ps.setString(8, batch);
19+
ps.setString(9, sessionId);
20+
ps.execute();
21+
}
22+
} catch (SQLException e) {
23+
System.out.println("SQLException: " + e.getMessage());
24+
System.out.println("SQLState: " + e.getSQLState());
25+
System.out.println("VendorError: " + e.getErrorCode());
26+
}
27+
28+
}
29+
30+
protected static void syncLocalDataToRemote() {
31+
String appDataPath = System.getenv("APPDATA") + "\\SessionTracker";
32+
String dbPath = appDataPath + "\\user_sessions.db"; // Path to SQLite database
33+
String localDbUrl = "jdbc:sqlite:"+ dbPath; // Your local database;
34+
String selectSql = "SELECT * FROM sessions";
35+
36+
try (Connection localConn = DriverManager.getConnection(localDbUrl);
37+
PreparedStatement ps = localConn.prepareStatement(selectSql);
38+
ResultSet rs = ps.executeQuery()) {
39+
40+
while (rs.next()) {
41+
insertDataRemote(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9));
42+
43+
}
44+
} catch (SQLException e) {
45+
System.out.println("SQLException: " + e.getMessage());
46+
System.out.println("SQLState: " + e.getSQLState());
47+
System.out.println("VendorError: " + e.getErrorCode());
48+
}
49+
}
50+
}

Main.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import javax.swing.SwingUtilities;
2+
3+
public class Main{
4+
public static void main(String [] args){
5+
new AppBackend().setupDatabase();
6+
CloudDatabaseUpload.syncLocalDataToRemote();
7+
SwingUtilities.invokeLater(MyFrame::new);
8+
}
9+
}

MyFrame.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
7+
class MyFrame extends JFrame implements ActionListener {
8+
MyPanel p1,p2;
9+
MyFrame() {
10+
p1 = new MyPanel("Log-In");
11+
p1.jb1.addActionListener(this);
12+
p1.jb2.addActionListener(this);
13+
this.getContentPane().setLayout(new GridBagLayout());
14+
this.getContentPane().add(p1);
15+
this.setResizable(false);
16+
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
17+
this.setUndecorated(true);
18+
this.setAlwaysOnTop(true);
19+
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
20+
this.setLocationRelativeTo(null);
21+
this.setVisible(true);
22+
}
23+
@Override
24+
public void actionPerformed(ActionEvent ae){
25+
String cmd = ae.getActionCommand();
26+
if("login_submit".equals(cmd)) {
27+
String Username = p1.tf1.getText().trim();
28+
String USN = p1.tf2.getText();
29+
if(ValueCheck(USN,Username)) {
30+
//AppBackend ab1 = new AppBackend();
31+
new AppBackend().insertData(Username,USN);
32+
CloudDatabaseUpload.syncLocalDataToRemote();
33+
System.exit(0);
34+
}
35+
else
36+
JOptionPane.showMessageDialog(this,"Invalid input");
37+
}
38+
else if("admin_panel".equals(cmd)) {
39+
this.getContentPane().remove(p1);
40+
p2 = new MyPanel();
41+
p2.jb3.addActionListener(this);
42+
p2.jb4.addActionListener(this);
43+
this.getContentPane().add(p2);
44+
this.revalidate();
45+
this.repaint();
46+
}
47+
else if("admin_submit".equals(cmd)) {
48+
String password = new String(p2.tf3.getPassword());
49+
if (password.equals("admin"))
50+
System.exit(0);
51+
else
52+
JOptionPane.showMessageDialog(this, "Wrong password");
53+
}
54+
else if("back".equals(cmd)) {
55+
this.getContentPane().remove(p2);
56+
this.getContentPane().add(p1);
57+
this.revalidate();
58+
this.repaint();
59+
}
60+
}
61+
protected boolean ValueCheck(String usn,String name) {
62+
if(usn.isEmpty() || name.isEmpty() || usn.length() != 10)
63+
return false;
64+
String pattern = "^1VI\\d{2}[A-Z]{2}\\d{3}$";
65+
return usn.toUpperCase().matches(pattern);
66+
67+
}
68+
}

MyPanel.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
4+
class MyPanel extends JPanel {
5+
JLabel l1,l2,l3,l4,l5;
6+
JButton jb1,jb2,jb3,jb4;
7+
JTextField tf1,tf2;
8+
JPasswordField tf3;
9+
MyPanel() {
10+
this.setLayout(null);
11+
this.setPreferredSize(new Dimension(420,420));
12+
l4 = new JLabel("ADMIN LOGIN");
13+
l4.setBounds(135,20,150,50);
14+
l4.setFont(l4.getFont().deriveFont(Font.BOLD));
15+
l4.setFont(l4.getFont().deriveFont(20f));
16+
l3 = new JLabel("Password");
17+
l3.setBounds(130,95,150,30);
18+
tf3 = new JPasswordField(15);
19+
tf3.setBounds(130,120,150,30);
20+
jb3 = new JButton("Submit");
21+
jb3.setBounds(130,170,150,30);
22+
jb3.setActionCommand("admin_submit");
23+
jb4 = new JButton("Back to Log in ?");
24+
jb4.setBounds(130,300,150,25);
25+
jb4.setBorderPainted(false);
26+
jb4.setContentAreaFilled(false);
27+
jb4.setForeground(Color.blue);
28+
jb4.setActionCommand("back");
29+
this.add(l3);
30+
this.add(l4);
31+
this.add(jb3);
32+
this.add(tf3);
33+
this.add(jb4);
34+
}
35+
MyPanel(String pname) {
36+
this.setName(pname);
37+
this.setLayout(null);
38+
this.setPreferredSize(new Dimension(420,420));
39+
tf1 = new JTextField();
40+
tf1.setBounds(130,120,150,25);
41+
tf2 = new JTextField();
42+
tf2.setBounds(130,170,150,25);
43+
l1 = new JLabel("Name");
44+
l1.setBounds(130,95,150,30);
45+
l2 = new JLabel("USN");
46+
l2.setBounds(130,145,150,30);
47+
l5 = new JLabel("LOGIN");
48+
l5.setBounds(155,20,150,50);
49+
l5.setFont(l5.getFont().deriveFont(Font.BOLD));
50+
l5.setFont(l5.getFont().deriveFont(30f));
51+
jb1 = new JButton("Submit");
52+
jb1.setBounds(130,220,150,25);
53+
jb1.setActionCommand("login_submit");
54+
jb2 = new JButton("Log In as Admin ?");
55+
jb2.setBounds(130,300,150,25);
56+
jb2.setBorderPainted(false);
57+
jb2.setContentAreaFilled(false);
58+
jb2.setForeground(Color.blue);
59+
jb2.setActionCommand("admin_panel");
60+
this.add(jb1);
61+
this.add(jb2);
62+
this.add(tf1);
63+
this.add(tf2);
64+
this.add(l1);
65+
this.add(l2);
66+
this.add(l5);
67+
}
68+
}

0 commit comments

Comments
 (0)