|
| 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 | +} |
0 commit comments