Answer to Question #228491 in Java | JSP | JSF for umme habiba sumi

Question #228491

 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).]


1
Expert's answer
2021-08-24T07:21:19-0400

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);}  
  
}  
}  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS