使用Java语言编制JDBC程序联接Oracle数据库-------------- [@more@]
使用JBuilder 10.0.1 Foundtion Edtion编写,代码如下:
package accessdatabase;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2004
*
Company:
* @author not attributable
* @version 1.0
*/
/**
*this class using to get the connection with the used database for other class
*used to get the select or insert or update result from the resultset.
*/
import javax.sql.*;
import java.sql.*;
import java.net.URL;public class ConnSql { public ConnSql(){}
private static boolean checkForWarning(SQLWarning warn)
throws SQLException
{ boolean rc= false; //if SQl Warning object is not null,its means has catch the exception.
//display the relation information.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());
System.out.println("");
warn=warn.getNextWarning();
}
}
return rc; } //===========================
//dispResultSet
//============================
private static void dispResultSet(ResultSet rs)
throws SQLException
{ int i; //get the resultSet of medata.ResultSetMetaData rsmd=rs.getMetaData(); //catch the resultSet's hash column.int numCols = rsmd.getColumnCount(); //display title.for(i=1;i<=numCols;i++){
if(i>1)System.out.print(",");
System.out.println(rsmd.getColumnLabel(i));
}
System.out.println(""); //display all data in the resultSet.
boolean more = rs.next();
while(more){ //do for while to display the all data in the resultSet.for(i=1;i<=numCols;i++){
if(i>1)System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println(""); //catch the next row data.more=rs.next();
} } public static void main(String args[]) {
String sUsr="xxg";
String sPwd="xxg";
String query="select * from xxg";
try{ //add jdbc-odbc briger driver.Class.forName("oracle.jdbc.driver.OracleDriver");
//set a note streamer for noted the execute procedure for artlog the JDBC.
DriverManager.setLogStream(System.out); //conect to the Driver program.the Driver which system has registered,
//will be connected and added.
//end when get a right Driver.Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xxg",sUsr,sPwd);//when go here is mean connection is sccessful.
//check and display the artlen and error message.
checkForWarning(con.getWarnings()); //create a Statement object,using its to set SQL query to the Driver.Statement stmt = con.createStatement(); //taken the SQL query,create a ResultSet object.
ResultSet rs = stmt.executeQuery(query); //display the resultset for all roe and column.dispResultSet(rs); //close the ResultSet.rs.close(); //close statement object.stmt.close();//close connection.
}catch(SQLException ex) { //catch error message,display them.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) { //catch other class's exception.ex.printStackTrace();
}}
}