Advertisement
Advertisement
| 04.14.2008 at 07:20PM PDT, ID: 23322494 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class createTable
{
public static void main(String[] args) throws SQLException
{
DB db = new DB();
/*
try {
Class.forName("com.quadcap.jdbc.JdbcDriver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
*/
String username;
String password;
username = "something";
password = "123456";
Connection conn = db.dbConnect(username, password);
//Connection conn = java.sql.DriverManager.getConnection("jdbc:qed:mynewdb", p);
try{
Statement st = conn.createStatement();
//BufferedReader bf = new BufferedReader
//(new InputStreamReader(System.in));
//System.out.println("Enter Database name:");
//String database = bf.readLine();
//st.executeUpdate("CREATE DATABASE "+database);
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
catch (Exception e){
e.printStackTrace();
}
//DB db = new DB();
//conn=db.dbConnect("jdbc:qed:mynewdb", "p");
db.createTables(conn);
}
}
class DB
{
public DB() {}
public Connection dbConnect(String db_connect_string,
String db_userid)
{
try
{
Class.forName("com.quadcap.jdbc.JdbcDriver");
java.util.Properties p = new java.util.Properties();
p.setProperty("create", "true");
java.sql.Connection conn =
java.sql.DriverManager.getConnection("jdbc:qed:mynewdb", p);
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
//Employee table
public void createTables(Connection conn)
{
String query;
Statement stmt = null;
try
{
query="create table employees " +
"(name varchar(32), " +
"id varchar(50), " +
"telephoneNo varchar(50), " +
"email varchar(50), " +
"JOB_ID integer, " +
"foreign key (JOB_ID) references Job_roles (JOB_ID))";
stmt.executeUpdate("INSERT INTO employees " +
"VALUES ('John Doe', '1', '5555999944', 'Something@mail.com', 1)");
stmt.executeUpdate("INSERT INTO employees " +
"VALUES ('Neil Mayhem', '2', '555588888', 'sth@mail.com', 2)");
stmt.executeUpdate("INSERT INTO employees " +
"VALUES ('Alice Lovelace', '3', '6666999944', 'alice@mail.com', 3)");
stmt.executeUpdate("INSERT INTO employees " +
"VALUES ('Jay Something', '4', '666669999', 'mail@mail.com',4 )");
stmt.executeUpdate("INSERT INTO employees " +
"VALUES ('Terry Something', '5', '622211111', 'mail20@mail.com', 1)");
stmt = conn.createStatement();
stmt.executeUpdate(query);
stmt.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
} //createTables
public void createJobRoles(Connection conn)
{
String query;
Statement stmt = null;
try
{
query="create table Job_roles " +
"(JOB_ID INTEGER NOT NULL, " +
"job_title varchar(32), " +
"salary_class varchar(50), " +
"job_description varchar(50), " +
"primary key(JOB_ID))";
stmt.executeUpdate("INSERT INTO Job_roles " +
"VALUES (1, 'Garden Worker', '4', 'Cultivates and cares for plants')");
stmt.executeUpdate("INSERT INTO Job_roles " +
"VALUES (2, 'Security Guard', '4', 'Takes care of security')");
stmt.executeUpdate("INSERT INTO Job_roles " +
"VALUES (3, 'Stage Hypnotist', '2', 'Hypnotizes people on stage')");
stmt.executeUpdate("INSERT INTO Job_roles " +
"VALUES (4, 'Amusement Park Guide', '3', 'Guides people through an amusement park')");
stmt = conn.createStatement();
stmt.executeUpdate(query);
stmt.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void printFields()
{
String query;
Statement stmt;
try {
query = "SELECT employees.name, job_roles.salary_class FROM employees INNER JOIN job_roles on employees.job_id = job_roles.job_id";
Connection conn = null;
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
//print query
System.out.printf("Salary of %s is %s\n", rs.getString(1), rs.getObject(2).toString());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void addColumn()
{
String query;
Statement stmt;
Connection conn = null;
try {
query= "ALTER TABLE employees ADD JOB_ID";
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void EmployeeNameSearch(String[] args) throws SQLException
{
List names = Arrays.asList(args);
List not = new ArrayList();
//Collections.sort(names);
PreparedStatement ps = null;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String option = stdin.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
for (Object name : names) {
try {
ps.setString(1, name.toString());
} catch (SQLException e) {
e.printStackTrace();
}
ResultSet rs = null;
try {
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
if (rs.next()) {
// name in dba
}
else
{
System.out.println(name + "not in db!");
not.add(name);
}
}
}
}
};
|