Click here to download this Java code.
import java.sql.*;
/*
This java class create four tables with all theirs fields and insert them
inside a mysql database name timelog. This code works with MySql Database with
Mysql Java Connector the mysql-connector-java-3.2.0-alpha
*/
public class MetaData {
public static void main(String args[]) {
/*following line initialize a variable to be used for database connection
later on in this program. you must change this line properly according to
your database name, username, and password.*/
String url = "jdbc:mysql://localhost/timelog?user=root&password=shaz";
/* The above statement can be written as follows if you do not use user name and password
String url = "jdbc:mysql://localhost/timelog"; */
Connection con;
String createActivity;
String createContact;
String createTimeLog;
String createEmployee;
createActivity = "create table ACTIVITY " +
"(activity_id integer not null unique, " +
"activity_name varchar(20), " +
"is_predefined char(1), " +
"task_name varchar(20), " +
"emp_id varchar(8)," +
"primary key(activity_id))";
createContact = "create table CONTACT " +
"(contact_id varchar(5) not null unique, " +
"contact_name varchar(20), " +
"phone varchar(12), " +
"email varchar(20), " +
"rating varchar(5)," +
"primary key(contact_id))";
createTimeLog = "create table TIMELOG " +
"(timelog_id integer not null unique, " +
"start_time timestamp, " +
"end_time timestamp, " +
"break_duration integer, " +
"duration integer," +
"emp_id varchar(8)," +
"contact_id varchar(5)," +
"activity_id integer," +
"description varchar(20)," +
"primary key(timelog_id))";
createEmployee = "create table EMPLOYEE " +
"(emp_id varchar(8) not null unique, " +
"emp_name varchar(20), " +
"primary key(emp_id))";
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver"); // format from MySQL Connector/J
} catch(java.lang.ClassNotFoundException e) {
System.err.println("ClassNotFoundException: "+ e.getMessage());
}
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
stmt.executeUpdate(createActivity);
stmt.executeUpdate(createContact);
stmt.executeUpdate(createTimeLog);
stmt.executeUpdate(createEmployee);
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
|
|
|
|