Question
Create a GUI application that allows the user to add new employees to the Personnel database created by this application in Java: import java.sql.*; public
Create a GUI application that allows the user to add new employees to the Personnel database created by this application in Java:
import java.sql.*; public class DatabaseCreator { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating database..."); stmt = conn.createStatement(); String sql = "CREATE DATABASE Personnel"; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); System.out.println("Creating table in given database..."); sql = "CREATE TABLE Employee " + "(id INTEGER not NULL, " + " name VARCHAR(255), " + " position VARCHAR(255), " + " pay_rate INTEGER, " + " PRIMARY KEY ( id ))"; stmt.executeUpdate(sql); System.out.println("Created table in given database..."); String query = " insert into Employee (id, name, position, pay_rate)" + " values (?, ?, ?, ?)"; PreparedStatement preparedStmt = conn.prepareStatement(query); preparedStmt.setInt(1, 1); preparedStmt.setString(2, "John"); preparedStmt.setString(3, "Manager"); preparedStmt.setInt(4, 100); preparedStmt.execute(); preparedStmt = conn.prepareStatement(query); preparedStmt.setInt(1, 2); preparedStmt.setString(2, "Smith"); preparedStmt.setString(3, "Accountant"); preparedStmt.setInt(4, 200); preparedStmt.execute(); preparedStmt = conn.prepareStatement(query); preparedStmt.setInt(1, 3); preparedStmt.setString(2, "Barry"); preparedStmt.setString(3, "Seceretary"); preparedStmt.setInt(4, 150); preparedStmt.execute(); preparedStmt = conn.prepareStatement(query); preparedStmt.setInt(1, 4); preparedStmt.setString(2, "Hary"); preparedStmt.setString(3, "Programmer"); preparedStmt.setInt(4, 250); preparedStmt.execute(); preparedStmt = conn.prepareStatement(query); preparedStmt.setInt(1, 4); preparedStmt.setString(2, "Sally"); preparedStmt.setString(3, "Engineer"); preparedStmt.setInt(4, 250); preparedStmt.execute(); conn.close(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started