• We just launched and are currently in beta. Join us as we build and grow the community.

Database Programming with JDBC

shaherul674

AR/VR Specialist
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
136
Likes
177
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Submitted by
coderz

on
Wed, 06/17/2009 - 08:16

Type 4 drivers or Native protocol driver is known to be the one with the best features and functions. Type 4 driver does not have any native methods and is a complete Java driver. You need not to install it on the client before use and the can be easily downloaded or configured on a system very easily. One of the best features of Type 4 driver is that it interacts directly with the DBMS server.

Firstly, you need to register the type of driver that you are using by using the string,

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

And then connect to the database by use of the connection string:

Connection con = DriverManager.getConnection(url, uid, password);

Here, url is the JDBC url, uid is the username for the database and its password.

Example for Database connection:

  1. import

    java.sql.Connection

    ;
  2. import

    java.sql.DriverManager

    ;
  3. import

    java.sql.SQLException

    ;


  4. public

    class

    Hello {
  5. static

    public

    void

    main(

    String

    args[

    ]

    )

    {
  6. Connection

    connection =

    null

    ;
  7. if

    (

    args.length

    !=

    4

    )

    {
  8. System

    .out

    .println

    (

    "Incorrect Syntax!"

    )

    ;
  9. return

    ;
  10. }
  11. try

    {

    // Loading driver
  12. Class

    .forName

    (

    args[

    0

    ]

    )

    ;
  13. }
  14. catch

    (

    Exception

    e )

    {

    //Print error message if problem with driver loading
  15. e.printStackTrace

    (

    )

    ;
  16. return

    ;
  17. }
  18. try

    {
  19. connection =

    DriverManager

    .getConnection

    (

    args[

    1

    ]

    , args[

    2

    ]

    , args[

    3

    ]

    )

    ;
  20. System

    .out

    .println

    (

    "You have connected to the database successfully!"

    )

    ;

  21. }
  22. catch

    (

    SQLException

    e )

    {
  23. e.printStackTrace

    (

    )

    ;
  24. }
  25. finally

    {
  26. try

    {
  27. connection.close

    (

    )

    ;
  28. }
  29. catch

    (

    SQLException

    e )

    {
  30. e.printStackTrace

    (

    )

    ;
  31. }
  32. }
  33. }

So, next time when you are planning for database programming then use JDBC in your application to make it simple and easy to use.

 

440,010

316,559

316,568

Top