Connection of Database with JAVA on Netbeans IDE:Step by Step

Connect Your database with Java
1)Go to services
2)Right click on database.Select new connection the following 
  window will open.(pic1)

pic1

3)Here just click on add .
4)After this ,add drivers.
5)Select drivers from your system.
6)Drivers are namely."Derby and derby client".
7)If you dont have driver .Try downloading DB2 
  software from  following link.

link   :1)http://db.apache.org/derby/releases/release-10.1.1.0.html

2)http://www.oracle.com/technetwork/java/javadb/downloads/index.html

8)After adding these drivers.
9)Go to "Servers", Add server.
10)Select server from the list.(pic2)

pic2

11)Click on next .Finish.(pic3)

pic3

12)Now your server is ready .Just you need to start it .
13)Right click on the selected server and start it.(pic4)

pic4

14)You can create a connection to the database:

   14.1)Right Click on the driver select as shown in figure.(pic5)
        
   14.2)Connect it
   14.3)Click on "+" sign there ,the drop down menu will open.
        Now Open tables(pic6)

pic6

   14.4)Right click on Table and now you can create  your own  table.(pic7)

pic7

15)Now the database connection in Coding:
   15.1)Write the below given code in your function,

    Connection con;
    ResultSet rs;
    PreparedStatement ps;
    java.sql.Statement stmt;

    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    con = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app","app");
    stmt =  con.createStatement();

    

15.2)Now you will be able to use your databse
15.3)For running the Query use the following code:

     public   ResultSet setResult() throws SQLException
 {
   ps=con.prepareStatement("select * from studetail");  //Studetail is the name of table
   rs = ps.executeQuery();
   return (rs);
 }
     

16)Many Function can be used on the basis of above connection.
17)Same type query can be used for other database connection 
just by altering the code.

Class.forName("oracle.jdbc.OracleDriver");
Connection con = 
        DriverManager.getConnection("jdbc:oracle:thin:@VITORA:1521:orcl", "username","password");

      

(Note:The content in the getconnection() can be copied from the 
properties of driver added)
18)Now we can apply respective update ,delete,new ,Show functions

Thanks for following
Contact me :
ankit91verma@gmail.com

Advertisements