à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday 8 October 2017

JDBC in JAVA 6


JDBC in JAVA 6

Connecting to MySQL Database using Thin Driver

To connect a Java application with MySQL database using Thin Driver. You need to follow the following steps,

1. Load Driver Class: The Driver Class for MySQL database is com.mysql.jdbc.Driver and Class.forName("com.mysql.jdbc.Driver") method is used to load the driver class for MySQL database.

2. Create Connection: For creating a connection you will need a Connection URL. The Connection URL for MySQL is

jdbc:oracle:thin:@localhost:1521:XE

jdbc - API
mysql - Database
localhost - Server name on which My SQL is running
1008 - Port Number
test - database name

You will also require Username and Password of your MySQL Database Server for creating connection.

3. Loading jar file: To connect your java application with MySQL, you will also need to load mysql-connector.jar file. This file can be loaded into 2 ways.

  1. Copy the jar file into C:\Program Files\Java\jre7\lib\ext folder. or,

  2. Set it into classpath.

Example

Create a table in MySQL Database

create table Student(sid number(10),sname varchar2(20))

Insert some record into the table

insert into Student values(101,'vraj');
insert into Student values(102,'abhi');

Accessing record from Student table in Java application 

import java.sql.*;
class Test{
      public static void main(String []args){
           try{
                 //Loading driver
                 Class.forName("com.mysql.jdbc.Driver");

                 //creating connection
                 Connection con = DriverManager.getConnection
                 ("jdbc:mysql:/ /localhost:3306/test","username","password");

                 Statement s = con.createStatement();	//creating statement

                 ResultSet rs = s.executeQuery("select * from Student");	//executing statement

                 while(rs.next()){
                      System.out.println(rs.getInt(1)+" "+rs.getString(2));
                 }

                 con.close();	//closing connection
               }catch(Exception e){
                 e.printStacktrace();
           }
    }
}

Output
102 vraj
103 abhi

Inserting record into a table using java application

import java.sql.*;
class Test{
         public static void main(String []args){
             try{
                     //Loading driver
                     Class.forName("com.mysql.jdbc.Driver");

                     //creating connection
                     Connection con = DriverManager.getConnection
                     ("jdbc:mysql:/ /localhost:3306/test","username","password");

                     PreparedStatement pst=con.prepareStatement("insert into Student values(?,?)");
         
                     pst.setInt(1,104);
                     pst.setString(2,"Alex");
                     pst.executeUpdate();

                     con.close();	//closing connection
                 }catch(Exception e){
                     e.printStacktrace();
                 }
       }
}

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website