D
dxd0222
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的代码,用:java.exe Example JDBC:ORACLE:ORACLE Scott tiger
或者用:java.exe JDBC:ODBCO scott tiger
(注:DO使我的ODBC数据源名称)
调用都产生错误:no suitable driver
我是否要设置其他的东西,请帮忙!!!!
import java.sql.*;
class Example
{
Connection oracleConn;
public static void main(String arg[])
{
if(arg.length < 3)
{
System.err.println("Example use:");
System.err.println("java Example <url> <username> <password>");
System.exit(1);
}
Example ex = new Example();
ex.initdb(arg[0],arg[1],arg[2]);
ex.testdb();
}
public void initdb(String url, String user, String passwd)
{
try
{
System.out.println(url + " " + user + " " + passwd);
oracleConn = DriverManager.getConnection(url,user,passwd);
}
catch(SQLException e)
{
System.err.println("Database connection failed: ");
System.err.println(e.getMessage());
System.exit(2);
}
}
public void testdb()
{
String custID = null;
Customer cust = new Customer(oracleConn);
try
{
custID = cust.insertNewCustomer(
"Jones", "Bill", "555-1234", "5 Main Street", "01234");
}
catch(SQLException e)
{
System.err.println("Insert failed:");
System.err.println(e.getMessage());
System.exit(3);
}
try
{
CustomerInfo info = cust.getCustomer(custID);
}
catch(SQLException e)
{
System.err.println("read failed;");
System.err.println(e.getMessage());
System.exit(4);
}
}
}
//public
class Customer
{
private Connection conn;
private PreparedStatement insertNewCustomer;
private CallableStatement getNewID;
public static CallableStatement checkZip;
public static CallableStatement checkID;
public Customer(Connection c)
{
conn = c;
try
{
insertNewCustomer = conn.prepareStatement("INSERT INTO customers VALUES(?,?,?,?,?,?)");
getNewID = conn.prepareCall("{call getNewID(?)}");
checkID = conn.prepareCall("{call checkID(?,?)}");
checkZip = conn.prepareCall("{call checkZip(?,?)}");
}catch(SQLException e)
{
System.err.println("Connot create statements");
}
}
public String insertNewCustomer(String lname,String fname,String pnum,String addr,String Zip)
throws InsertFailedException, SQLException
{
String newID;
if((newID = getNewID()) == null)
{
throw new InsertFailedException("could not get new ID");
}
insertNewCustomer.setString(1,newID);
insertNewCustomer.setString(2,lname);
insertNewCustomer.setString(3,fname);
insertNewCustomer.setString(4,pnum);
insertNewCustomer.setString(5,addr);
insertNewCustomer.setString(6,Zip);
if(insertNewCustomer.executeUpdate() != 1) {
throw new InsertFailedException("Could not execute insert");
}
return(newID);
}
public CustomerInfo getCustomer(String custID)
throws SelectException, SQLException{
if(!validateID(custID)) {
throw new SelectException("no customer with ID: " + custID);
}
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Customer WHERE CustID = " + custID);
CustomerInfo info = new CustomerInfo();
info.CustomerID = rs.getString(1);
info.LastName = rs.getString(2);
info.FirstName = rs.getString(3);
info.PhoneNumber = rs.getString(4);
info.StreetAddress = rs.getString(5);
info.Zip = rs.getString(6);
return(info);
}
public static synchronized boolean validateZip(String Zip)
throws SQLException {
checkZip.setString(1,Zip);
checkZip.registerOutParameter(2,Types.BIT);
checkZip.executeUpdate();
return(checkZip.getBoolean(2));
}
public static synchronized boolean validateID(String id)
throws SQLException {
checkID.setString(1,id);
checkID.registerOutParameter(2,Types.BIT);
checkID.executeUpdate();
return(checkID.getBoolean(2));
}
private String getNewID() throws SQLException {
getNewID.registerOutParameter(1,Types.VARCHAR);
getNewID.executeUpdate();
return(getNewID.getString(1));
}
}
class InsertFailedException extends SQLException
{
public InsertFailedException() {}
public InsertFailedException(String reason) {super(reason);}
}
class SelectException extends SQLException
{
public SelectException() {}
public SelectException(String reason) {super(reason);}
}
class CustomerInfo
{
String CustomerID;
String LastName;
String FirstName;
String PhoneNumber;
String StreetAddress;
String Zip;
}
或者用:java.exe JDBC:ODBCO scott tiger
(注:DO使我的ODBC数据源名称)
调用都产生错误:no suitable driver
我是否要设置其他的东西,请帮忙!!!!
import java.sql.*;
class Example
{
Connection oracleConn;
public static void main(String arg[])
{
if(arg.length < 3)
{
System.err.println("Example use:");
System.err.println("java Example <url> <username> <password>");
System.exit(1);
}
Example ex = new Example();
ex.initdb(arg[0],arg[1],arg[2]);
ex.testdb();
}
public void initdb(String url, String user, String passwd)
{
try
{
System.out.println(url + " " + user + " " + passwd);
oracleConn = DriverManager.getConnection(url,user,passwd);
}
catch(SQLException e)
{
System.err.println("Database connection failed: ");
System.err.println(e.getMessage());
System.exit(2);
}
}
public void testdb()
{
String custID = null;
Customer cust = new Customer(oracleConn);
try
{
custID = cust.insertNewCustomer(
"Jones", "Bill", "555-1234", "5 Main Street", "01234");
}
catch(SQLException e)
{
System.err.println("Insert failed:");
System.err.println(e.getMessage());
System.exit(3);
}
try
{
CustomerInfo info = cust.getCustomer(custID);
}
catch(SQLException e)
{
System.err.println("read failed;");
System.err.println(e.getMessage());
System.exit(4);
}
}
}
//public
class Customer
{
private Connection conn;
private PreparedStatement insertNewCustomer;
private CallableStatement getNewID;
public static CallableStatement checkZip;
public static CallableStatement checkID;
public Customer(Connection c)
{
conn = c;
try
{
insertNewCustomer = conn.prepareStatement("INSERT INTO customers VALUES(?,?,?,?,?,?)");
getNewID = conn.prepareCall("{call getNewID(?)}");
checkID = conn.prepareCall("{call checkID(?,?)}");
checkZip = conn.prepareCall("{call checkZip(?,?)}");
}catch(SQLException e)
{
System.err.println("Connot create statements");
}
}
public String insertNewCustomer(String lname,String fname,String pnum,String addr,String Zip)
throws InsertFailedException, SQLException
{
String newID;
if((newID = getNewID()) == null)
{
throw new InsertFailedException("could not get new ID");
}
insertNewCustomer.setString(1,newID);
insertNewCustomer.setString(2,lname);
insertNewCustomer.setString(3,fname);
insertNewCustomer.setString(4,pnum);
insertNewCustomer.setString(5,addr);
insertNewCustomer.setString(6,Zip);
if(insertNewCustomer.executeUpdate() != 1) {
throw new InsertFailedException("Could not execute insert");
}
return(newID);
}
public CustomerInfo getCustomer(String custID)
throws SelectException, SQLException{
if(!validateID(custID)) {
throw new SelectException("no customer with ID: " + custID);
}
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Customer WHERE CustID = " + custID);
CustomerInfo info = new CustomerInfo();
info.CustomerID = rs.getString(1);
info.LastName = rs.getString(2);
info.FirstName = rs.getString(3);
info.PhoneNumber = rs.getString(4);
info.StreetAddress = rs.getString(5);
info.Zip = rs.getString(6);
return(info);
}
public static synchronized boolean validateZip(String Zip)
throws SQLException {
checkZip.setString(1,Zip);
checkZip.registerOutParameter(2,Types.BIT);
checkZip.executeUpdate();
return(checkZip.getBoolean(2));
}
public static synchronized boolean validateID(String id)
throws SQLException {
checkID.setString(1,id);
checkID.registerOutParameter(2,Types.BIT);
checkID.executeUpdate();
return(checkID.getBoolean(2));
}
private String getNewID() throws SQLException {
getNewID.registerOutParameter(1,Types.VARCHAR);
getNewID.executeUpdate();
return(getNewID.getString(1));
}
}
class InsertFailedException extends SQLException
{
public InsertFailedException() {}
public InsertFailedException(String reason) {super(reason);}
}
class SelectException extends SQLException
{
public SelectException() {}
public SelectException(String reason) {super(reason);}
}
class CustomerInfo
{
String CustomerID;
String LastName;
String FirstName;
String PhoneNumber;
String StreetAddress;
String Zip;
}