Java MySQL Medicine Database Program
Java MySQL Medicine Database Program
Transaction management could improve the program by ensuring that all database operations, such as table creation and record insertion, are completed successfully before committing changes. By using `conn.setAutoCommit(false)` and implementing `conn.commit()` after successful operations, it would prevent partial database state updates and allow for rollback capabilities in case of failures, improving data integrity and consistency .
The Java program is designed with predefined SQL INSERT statements that specify exact medicine records to be entered into the database, seen in `insertRecord1`, `insertRecord2`, and `insertRecord3`. This design simplifies testing and ensures that a consistent dataset is available for retrieval. The on-screen results from executing this program are lines displaying each medicine's data - "Aspirin, 2022-01-01, 2023-01-01, Pain relief", "Tylenol, 2022-02-01, 2023-02-01, Fever reducer", and "Benadryl, 2022-03-01, 2023-03-01, Antihistamine" .
The Java program can utilize `Scanner` or a similar input stream to take user inputs during runtime. Before executing each `INSERT INTO` statement, it can prompt the user to enter medicine details through the console, with `scanner.next()` capturing inputs for fields like `medicine_name`, `manufactured_date`, `expiry_date`, and `medicine_uses`. This input can then be concatenated into a dynamic SQL insert command which `executeUpdate` would process, thereby replacing hard-coded values with user-driven data entry .
The SQL command used to insert new medicine records is `INSERT INTO medicine VALUES (...)`. In the Java application, strings representing each record are defined, such as `insertRecord1`, `insertRecord2`, `insertRecord3`, and these are executed by calling `stmt.executeUpdate` for each insert statement .
The Java program connects to the MySQL database using the `DriverManager.getConnection` method with the URL `jdbc:mysql://localhost:3306/medicine_db`, the user name `root`, and the password `password`. It then creates a `Statement` object to execute SQL queries. The program checks if the medicine table exists and creates it with the SQL command `CREATE TABLE IF NOT EXISTS Medicine` using the `stmt.executeUpdate` method if it does not exist .
The Java program retrieves records by executing the SQL query `SELECT * FROM medicine` using `stmt.executeQuery`, which returns a `ResultSet`. It iterates over the `ResultSet` using `rs.next()`, extracting each field's data with `rs.getString(field_name)` methods. It then displays the data by concatenating and printing these values in a formatted string .
In the MySQL table, dates are handled as `DATE` data types, reflecting a straightforward date storage format. In the Java program, date fields are retrieved from the database as strings using `rs.getString("field_name")`, which converts the SQL DATE format into a string representation that can be easily printed and manipulated. This typifies the conversion between database storage formats and Java's data handling, ensuring compatibility and ease of use .
The `CREATE TABLE IF NOT EXISTS` SQL command allows the program to conditionally create a table only if it doesn't already exist, preventing potential errors or exceptions from trying to create a duplicate table. This is a proactive measure that ensures the program can run multiple times without reinitialization errors, enhancing its robustness and flexibility in a production environment .
Java exceptions, specifically `SQLException`, are used to manage database connectivity issues. The `try` block wraps the connection, table creation, and data retrieval logic, while the `catch(SQLException e)` block handles any SQL-related errors, printing the stack trace for debugging. This prevents the program from crashing and provides useful information for resolving connectivity issues .
The current Java program is vulnerable to several security risks such as SQL injection, hardcoded credentials, and lack of encryption. Directly concatenated SQL commands can be exploited with injection attacks because user input is not sanitized. The hardcoded username and password offer an easy attack vector if accessed by unauthorized users. Additionally, the connection string lacks SSL encryption, exposing data to potential interception during transmission. Implementing parameterized queries, environment variable-based credential management, and enabling SSL can mitigate these vulnerabilities .