In Java 1. Write an application that creates the database Personnel with 1 table
ID: 3720261 • Letter: I
Question
In Java
1. Write an application that creates the database Personnel with 1 table Employee
2. Write a GUI application that allows the user to add new employees to the Personnel database.
Add your name to the Title
Insert these records into the table Employee:
3. Write a GUI application that allows the user to look up an employee in the PERSONNEL database using EmployeeID.
The user can edit any of the employee data except the EmployeeID.
Demonstrate a sucessful edit by showing the table data before and after the edit.
Allow Nulls Column Name ? Employeeld Name Data Type char(10) char(25) char(30) float ? ? ? ? Position PayRateExplanation / Answer
Answer: See the code below:
1. Application to create database and table:
a) PersonnelDBDemo class: PersonnelDBDemo.java
---------------------------------------------------------
package databasedemo;
import java.sql.*;
/**
* PersonnelDBDemo
*
*/
public class PersonnelDBDemo {
//jdbc driver string
static final String jdbcDriver = "com.mysql.jdbc.Driver";
//database URL
static final String connectionURL = "jdbc:mysql://localhost";
//database username and password
//change it to your username and password
static final String username = "root";
static final String password = "test";
private Connection con; //database connection object
private boolean initialized; //flag to check if jdbc initialized
/**
* Constructor
*/
public PersonnelDBDemo() {
// TODO Auto-generated constructor stub
con = null;
initialized = false;
}
//initialize jdbc
public void init()
{
try{
//register jdbc driver
Class.forName(jdbcDriver);
//open connection
con = DriverManager.getConnection(connectionURL, username, password);
initialized = true;
}
catch(SQLException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//create database
public void createDatabase(String dbName)
{
//create database if initialized
if (initialized)
{
try {
Statement statement = con.createStatement();
System.out.println("Trying to create database " + dbName + "...");
String sqlString = "Create Database " + dbName;
statement.executeUpdate(sqlString);
System.out.println("Created database " + dbName + "...");
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//create table
public void createTable(String dbName, String tableName)
{
try {
//update connection to connect to given database
String databaseURL = connectionURL + "/" + dbName;
con = DriverManager.getConnection(databaseURL,username,password);
Statement statement = con.createStatement();
//sql query to create table
String query = "Create Table " + tableName +
"(EmployeeID char(10) not NULL, " +
"Name char(20), " +
"Position char(30), " +
"PayRate float, " +
"Primary Key (EmployeeID))";
//create table
System.out.println("Trying to create table " + tableName + "...");
statement.executeUpdate(query);
System.out.println("Created table " + tableName + "!!!");
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//terminate jdbc
public void terminate() throws SQLException
{
if (con != null)
{
con.close();
}
}
}
--------------------------------------------------------------
2. DatabaseDemo class: DatabaseDemo.java
-----------------------------------------------------
import java.sql.SQLException;
/**
* DatabaseDemo class
*
*/
public class DatabaseDemo {
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
String dbName = "Personnel";
String tableName = "Employee";
PersonnelDBDemo dbDemo = new PersonnelDBDemo();
dbDemo.init();
dbDemo.createDatabase(dbName);
dbDemo.createTable(dbName, tableName);
dbDemo.terminate();
}
}
-----------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.