Translate

Tuesday, 15 October 2013

Connecting Oracle with Netbeans IDE

I am not a good developer but i searched for this topic i mean "connecting netbeans with oracle"  after a long search i got this simple answer....explained below
 Sorry for my english...

Do this in a new operting system i means format all stuff ...since you may be installed and uninstalled netbeans and oracle number of times so there will be some problem but i dont know what problem it will be...better start with new operating system  if you wish or try it in the old one....

step to add oracle to netbeans

stage 1

first install netbeans 6.9 or other..
create one sample web application
run the application
internet exlpore will open( i.e now port 8080 will be occupied by glassfish server)

stage 2

download oracle 11g enterprise edtition
install the oracle 11 g( dont close the IE opened in netbeans since port 8080 should not be occupied be oracle)

at certain period of instalation oracle 11g port name will be asked at that time enter http port with 8087

then finish the installation

both oracle and netbeans can run at same time..

stage 3

How to add oracle to netbeans

first download ojdbc6.jar ans save it

then click add new driver--> browse the place in which you saved the ojdbc.jar

then view the driver list---> oracle thin will be displayed

then right clik on oracle thin driver---> click connect using  --a window will be opened

enter the following details (note keep open the oracle datbase in IE)

Host:   localhost
Port:   1521
SID:   XE
username: system
Password: password which you gave when installing oracle.
Check show JDBC url

then click ok button thats it

now you can see the jdbc:oracle:thin:@localhost:1521:XE(sysytem on SYSTEM)

Note ( add lib like ojdbc14.jar,ojjdbc6_g.jar,ojdbc6.jar in project properties and in oracle thin)


whatever table created in oracle will be displayed in netbeans..


stage 3 

coding


import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.activation.DataSource;
/**
 *
 * @author Administrator
 */
public class NewServlet extends HttpServlet {
  
    DataSource ds=null;
    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;

       
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {

   

        String Name= request.getParameter("Name");
        String Password= request.getParameter("Password");





        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","password");
        
           
            stmt=con.createStatement();
out.println("Connection Established");


 rs=stmt.executeQuery("select * from table name");

           
                 while(rs.next())
                 {
                      String str=rs.getString(1);

          out.println("<h1>"+str+"</h1>");
                 }
           
            con.close();
        }
        catch(SQLException e)
        {
            System.out.println(e.toString());
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }



       
    }

}

Comment if i said anything wrong but it worked for me :)