Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

oracle connection with jdbc

 
Steven Zeng
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a update class using jdbc,but the it can't establish the connection.This code attempt to connect a oracle database(v8.1.5):
//BatchUpdate.java
import java.sql.*;
import oracle.jdbc.driver.*;
public class BatchUpdate
{
public static void main(String args[]) throws SQLException
{
ResultSet rs = null;
PreparedStatement ps = null;
String url = "jdbc racle:thin:159.30.200.32:1521:fm";/*I am sure of the IP and databse name(fm) and port(1521)*/

Connection con;
Statement stmt;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try
{
con = DriverManager.getConnection(url,"fm", "fm");
con.setAutoCommit(false);
stmt = con.createStatement();
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Amaretto', 49, 9.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Hazelnut', 49, 9.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Amaretto_decaf', 49, 10.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES " +
"VALUES('Hazelnut_decaf', 49, 10.99, 0, 0)");
int [] updateCounts = stmt.executeBatch();
con.commit();
con.setAutoCommit(true);
ResultSet uprs = stmt.executeQuery("SELECT * FROM COFFEES");
System.out.println("Table COFFEES after insertion:");
while (uprs.next())
{
String name = uprs.getString("COF_NAME");
int id = uprs.getInt("SUP_ID");
float price = uprs.getFloat("PRICE");
int sales = uprs.getInt("SALES");
int total = uprs.getInt("TOTAL");
System.out.print(name + " " + id + " " + price);
System.out.println(" " + sales + " " + total);
}
uprs.close();
stmt.close();
con.close();
}
catch(BatchUpdateException b)
{
System.err.println("-----BatchUpdateException-----");
System.err.println("SQLState: " + b.getSQLState());
System.err.println("Message: " + b.getMessage());
System.err.println("Vendor: " + b.getErrorCode());
System.err.print("Update counts: ");
int [] updateCounts = b.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++)
{
System.err.print(updateCounts[i] + " ");
}
System.err.println("");
}
catch(SQLException ex)
{
System.err.println("-----SQLException-----");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
System.err.println("Vendor: " + ex.getErrorCode());
}
}
}
the running result is :
-----SQLException-----
SQLState: null
Message: The Network Adapter could not establish the connection
Vendor: 0
But using the Sql Plus of Oracle I can connect the database without any matter.Can anybody tell me how to do?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try:
jdbcracle:thin:@ipadress:1521:ORACLESID
you forgot the '@' in your url
[ August 05, 2002: Message edited by: Oliver Refle ]
 
Steven Zeng
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if i add @ in the ur,i will get this exception:
java.lang.AbstractMethodError
 
Steven Zeng
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Oliver, I am sorry, you are right, I have connected the database, thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic