Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
/*
-This demo application uses the Oracle JDBC thin driver to execute a query
against an Oracle database.
It has been tested
successfully using Eclipse with the following components:
Oracle 11g
Oracle Database 11g Release 1 (11.1.0.6.0) JDBC
Drivers (ojdbc5.jar and ojdbc6.jar)
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-111060-084321.html
Java version 1.6 ;
Using Oracle 10g, you can download the driver ojdbc14.jar from
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc101040-094982.html
The Employee table:
*/
package cn.com.java;
import java.util.Scanner;
import java.sql.*;
/**
* console sample for quering information of employee.
* @author zfu
*/
public class Employee{
//the driver of database
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
// the connection address of database
//Using SPSU oracle 10g, The URL is "jdbc:oracle:thin:@oracle3.spsu.edu:1521:orcl”
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl";
//the user name of database
// Using SPSU oracle 10g, the User is your SPSU id (i.e. the characters of your SPSU email
//address before the “@”).
public static final String DBUSER ="***";
// the password of database
// Using SPSU oracle 10g, Oracle account passwords are initially set to “temp00”.
public static final String DBPASS = " ***";
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(System.in);
String name = "";
String tbFname = "";
ResultSet result = null;
try{
//load the Oracle JDBC Driver.
Class.forName(DBDRIVER);
}catch(ClassNotFoundException e){
e.getMessage();
}
while(!name.toUpperCase().equals("QUIT")){
System.out.println("Please enter the employee's name or type 'Quit' to exit!");
name = scan.nextLine();
try{
//connect the Oracle.
Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
System.out.println("Connect successfully!");
String sql = "select FNAME,MINIT,LNAME,SSN,BDATE,ADDRESS,SEX,SALARY,SUPERSSN,DNO from
employee where FNAME = '"+name+"'";
Statement stmt = conn.createStatement();
result = stmt.executeQuery(sql);
while(result.next()){
tbFname = result.getString("FNAME");
String tbMinit = result.getString("MINIT");
String tbLname = result.getString("LNAME");
String tbSsn = result.getString("SSN");
String tbBDate = result.getString("BDATE");
String tbAddress = result.getString("ADDRESS");
String tbSex = result.getString("SEX");
String tbSalary = result.getString("SALARY");
String tbSuperssn = result.getString("SUPERSSN");
String tbDno = result.getString("DNO");
System.out.println("-----------------------------");
System.out.println("FNAME : " + tbFname);
System.out.println("MINIT : " + tbMinit);
System.out.println("LNAME : " + tbLname);
System.out.println("SSN : " + tbSsn);
System.out.println("BDATE : " + tbBDate);
System.out.println("ADDRESS : " + tbAddress);
System.out.println("SEX : " + tbSex);
System.out.println("SALARY : " + tbSalary);
System.out.println("SUPERSSN : " + tbSuperssn);
System.out.println("DNO : " + tbDno);
System.out.println("-----------------------------");
System.out.println(result.next());
}
if(tbFname.equals("")&&!name.toUpperCase().equals("QUIT")){
System.out.println("No person named " + name + " was found in the
current database. " + "\n");
}
result.close();
conn.close();
}catch(SQLException e){
e.getMessage();
}
}
}
}