Home » Developer & Programmer » JDeveloper, Java & XML » Testing JDBC Connection checkup
Testing JDBC Connection checkup [message #91984] Tue, 13 January 2004 02:11
Prasanna
Messages: 43
Registered: April 2001
Location: India
Member
/* hai the following java program gives error during compilation.The program is given below and the errors during compilation is also given below.Any Suggestions welcome.This program is given in oracle 9i complete reference in jdbc and jsql chapter.Thanks.*/
/*
* This sample can be used to check the JDBC installation.
* Just run it and provide the connect information. It will select
* " Hello World " from the database.
*/

// you need to import the java.sql package to use JDBC
import java.sql.*;

//We import java.io to be able to read from the command line
import java.io.*;

class JdbcCheckup
{

public static void main (String args[[]])
throws SQLException, IOException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

//Prompt the user for connect information
System.out.println("Please enter information to test connection to the"
+"database");
String user;
String password;
String database;


user = readEntry ("user: ");
int slash_index = user.indexOf ('/');
if (slash_index != -1)
{
password = user.substring (slash_index + 1);
user = user.substring (0,slash_index);
}
else {
password = readEntry ("password: ");
}
database = readEntry ("database (a TNSNAME entry): ");
System.out.print ("Connecting to the database...");

System.out.flush ();

System.out.println ("Connecting...");

Connection conn =
DriverManager.getConnection("jdbc:oracle:oci8:@" + database,
user,password);
System.out.println("Connected.");


//Create a statement
Statement stmt = conn.createStatement();

// Run a query
ResultSet rset=stmt.executeQuery("select 'Hello World' from dual");
//loop thru the query results
while (rset.next ()) {
System.out.println (rset.getString (1));
}

System.out.println ("your JDBC installation is correct.");

// close the statement
stmt.close();

//close the connection
conn.close();
}

//Utility function to read a line from standard input
static String readEntry (String prompt)
{
try
{
StringBuffer buffer = new StringBuffer ();
System.out.print (prompt);
System.out.flush ();
int c = System.in.read ();
while (c != 'n' && c != -1)
{
buffer.append ((char)c);
c=System.in.read();
}
return buffer.toString ().trim ();
}
catch (IOException e)
{
return "";
}
}
}

/* The error during compilation is this following line indicating to typecast explicityly the conn object.
Connection conn =
DriverManager.getConnection("jdbc:oracle:oci8:@" + database,
user,password);
*/
Previous Topic: What is the BEST way to return a result set from PL/SQL
Next Topic: Java - JDBC& oracle
Goto Forum:
  


Current Time: Fri Apr 19 21:12:20 CDT 2024