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.