import java.io.*;
import java.sql.*;
import java.net.URL;
public class java2oracle{
public static boolean checkForWarning(SQLWarning warn) throws SQLException{
boolean rc=false;
if(warn!=null){
System.out.println("\n***Warning***\n");
rc=true;
while(warn!=null){
System.out.println("SQLState:"+warn.getSQLState());
System.out.println("Message:"+warn.getMessage());
System.out.println("Vendor:"+warn.getErrorCode());
warn=warn.getNextWarning();
}
}
return rc;
}
public static void dispResultSet(ResultSet rs) throws SQLException{
int i;
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
for(i=1;i<=numCols;i++){
if(i>1) System.out.print(", ");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
boolean more=rs.next();
while(more){
for(i=1;i<=numCols;i++ ){
if(i>1) System.out.print(", ");
System.out.print(rs.getString(i));
}
System.out.println("");
more=rs.next();
}
}
/**
* @param args
*/
public static void main(String args[]) throws IOException{
// TODO 自动生成方法存根
String sUsr="system";
String sPwd="asdfgh";
while(true){
try{
System.out.print("SQL > ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
if(str.equalsIgnoreCase("end")) break;
if(str.equalsIgnoreCase("exit")) break;
if(str.equalsIgnoreCase("quit")) break;
if(str.endsWith(";")) str=str.substring(0,str.length()-1);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","asdfgh");
checkForWarning(con.getWarnings());
Statement stmt=con.createStatement();
if(str.toLowerCase().startsWith("select")){
ResultSet rs=stmt.executeQuery(str);
dispResultSet(rs);
rs.close();
}
else stmt.executeUpdate(str);
stmt.close();
con.close();
}
catch(SQLException ex){
System.out.println("\n***SQLException caught***\n");
while(ex!=null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
catch(java.lang.Exception ex){
ex.printStackTrace();
}
}
}
}