My professor asks us to make a java program that asks the user to input the data and the data inputted should automatically be inserted into a table in sql developer .
I have a background in Java and database but i'm not familiar with creating JDBC applications.
I searched a tutorial online and it said that i need to register the JDBC driver first. I ran the code in my compiler but it outputs Error: unable to load driver class!
.
What should i do for it to register?
I still don't know the concept of Class.forName();
Secondly, it errors a SQLException: No suitable drivers found
I don't know what's wrong in my code but the connection details in my SQL developer is this:
Connection name: mariel
Connection details: mariel@//localhost:1521/XEXDB
The code:
import java.sql.* ;
public class employeeMode{
public static void main(String args[]) throws SQLException{
String URL = "jdbc:oracle:thin:@localhost:1521:XEXDB";
String USER = "mariel";
String PASS = "1234";
Connection conn = DriverManager.getConnection(URL, USER, PASS);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
}
EDIT:
I fixed it! Thank you everyone! here's the working code:
import java.sql.* ;
public class employeeMode{
public static void main(String args[]) throws SQLException{
String URL = "jdbc:oracle:thin:mariel@//localhost:1521/XEXDB";
String USER = "mariel";
String PASS = "1234";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
}
@Mariel, have you installed Oracle 10g or any version Database, i think Class.forName("oracle.jdbc.driver.OracleDriver"); is unable to find any database connection which you have installed your system
download ojdbcxxx.jar file from below drive http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
Add a jar that goes with the database
for oracle the connection string is
jdbc:oracle:thin:
username
/password
@url
:port
:schema
for Mysql follow the link
First you have to load the driver and then create the connection as below. Also you have to put the ojdbc jar in the classpath.
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
import java.sql.* ;
public class employeeMode{
public static void main(String args[]) throws SQLException{
String URL = "jdbc:oracle:thin:@localhost:1521:XEXDB";
String USER = "mariel";
String PASS = "1234";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
}
catch(ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
}
You must have to add oracle driver jar file which is available on net http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html . then apply it in your class path.
First you try to make the connection using drivermanager before load your oracle driver first you should add oracle ojdbc6-11.2.0.4.jar file to load the oracle driver
you can separate your two methods one to open and the second to close your database connection as below you
public class BDConnection
{
private static final String DB_DRIVER = "jdbc:oracle:thin:@localhost:1521:XEXDB";
private static final String DB_CONNECTION = "jdbc:oracle:thin:@localhost:1521:XEXDB";
private static final String DB_USER = "mariel";
private static final String DB_PASSWORD = "1234";
}
public static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
return DriverManager.getConnection(
DB_CONNECTION, DB_USER, DB_PASSWORD);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
public static void closeMyConnection(Connection connection) {
try {
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
And now to test your code just you shulod call getDBConnection() in your main method after adding your jar file
public static void main(String[] argv) {
Connection connection =getDBConnection();
closeMyConnection(connection);
}
Isn't it
Class.forName("oracle.jdbc.OracleDriver");
? And:
...
try {
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(URL, USER, PASS);
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.