Problems and Solutions for connecting to Oracle using Java

Source: Internet
Author: User

Directly connect to the database using JDBC. The class file is as follows:

Import java. SQL .*;

Public class createtable {
Public static void main (string ARGs []) {
Connection con = NULL;
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
System. Out. println ("JDBC driver loaded ");
String url = "JDBC: oracle: thin :@192.16.0.99: 1521: jasv";// The red part is the IP address, port, and Sid of the server where Orale is located.

// (You can view Sid Through select * from V $ instance; where instance_name is Sid)
String user = "Chen ";
String Password = "mongojiang ";

Con = drivermanager. getconnection (URL, user, password );
System. Out. println ("database connection established ");

Statement stmt = con. createstatement ();
String UPD = "create table author_chj (author_id char (10), author_name char (50 ))";
Stmt.exe cuteupdate (UPD );
System. Out. println ("table-author_chj created ");
} Catch (classnotfoundexception cnfe ){
System. Out. println (cnfe );
} Catch (sqlexception e ){
System. Out. println ("sqlexcepiton:" + E );
} Catch (exception e ){
System. Out. println ("an unknown error occurred while conneting to the Database ");
} Finally {
Try {
If (con! = NULL ){
Con. Close ();
}
} Catch (sqlexception sqle ){
System. Out. println ("unable to close database connection .");
}
}
}
}

Compiled successfully with javac, but executed in browser, error:Java. Lang. classnotfoundexception: Oracle. JDBC. Driver. oracledriver.It can be inferred that the driver cannot be found. It may be that the classpath configuration is incorrect, or C :/Oracle/jdbc/libThe classes12.jar file is missing. First, configure classpath ------> my computer ----> right-click -----> properties -----> detailed ----> environment variable

Classpath -- SetC:/Oracle/jdbc/lib/classes12.jar; append the program. Run the program again.

If the network adapter cocould not establish the connection is abnormal, it may be because the IP address of the server is specified incorrectly or the firewall is doing something wrong.

Note: The Oracle client is installed on my machine.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.