How to connect Java application with Oracle and Mysql database using JDBC? Explain the complete procedure to connect the form of your question #228424 into your database using Oracle or Mysql. [Please write the reason for writing that line at the end of each line in the form of comments (for coding).]
Connecting Java application with Oracle and Mysql database using JDBC involves five steps:
1. Register the Driver class
2. Create connection
3. Create statement
4. Execute queries
5. Close connection
Example
import java.sql.*;
class Main{
public static void main(String args[]){
try{
//register the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//create the connection
Connection c=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//create the statement
Statement s=c.createStatement();
//execute query
ResultSet res=s.executeQuery("select * from emp");
while(res.next())
System.out.println(res.getInt(1)+" "+res.getString(2)+" "+res.getString(3));
//close the connection
c.close();
}
catch(Exception e){ System.out.println(e);}
}
}
Comments
Leave a comment