|
||||||
| Java Tutorials Tutorials and Code for Java |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
Java Database Connectivity
What is JDBC? JDBC is nothing but Java DataBase Connectivity. As the name suggests, it deals with connecting you code business logic to a database. Types of Connections There are basically 4 types of drivers which you can use to connect to the database: Type 1 : JDBC-ODBC bridge Type 2 : Partial Java driver Type 3 : pure Java driver for database middleware Type 4 : pure Java driver for direct-to-database The most widely used and supported by different vendors are the type 2 and type 4 drivers. They are easy to use and portable across various databases, and we will discuss the same here. ![]() How to use it 1) Declaring a Connection reference variable which will store the connection details. You do this by Code:
Connection variable_name; The class is loaded by using Class class's forname method. Code:
Class.forname("Path_of_the_class");
3) Once the driver is loaded, it is time to connect to the database. But to connect to it, you need the username and password and the ip ort number where the database server listens. Code:
variable_name=DriverManager.getConnection ("type_of_connectivity:database://ip_address:port_number/db_name", "username", "password");
The various objects that are used for SQL are:
The Code Note :: We are connecting to MySQL database. The database name is test and username and password is root. Code:
package com.mycomputer.connectDB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection
{
Connection conn;
public DBConnection()
{
try
{
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test", "root", "root");
System.out.println("\nConnected to the database");
}
catch(ClassNotFoundException e)
{
System.out.println("\nClass not found. Check Path");
e.printStackTrace();
}
catch (SQLException e)
{
System.out.println("Not connected");
e.printStackTrace();
}
}
public static void main(String[] args)
{
new DBConnection();
}
}
Code:
Connected to the database ![]()
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
![]() |
| Tags |
| database, java, jdbc, tutorial |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Storing Images in MySQL with PHP | Jordan | PHP Tutorials | 10 | 06-22-2008 07:02 PM |
| Tutorial: Starting Java Using Netbeans | Jordan | Java Tutorials | 0 | 04-05-2008 02:45 PM |
| Java tutorial : my sql with java | Arkie | Java Tutorials | 2 | 04-05-2008 12:51 PM |
| John's Java Tutorial Index | John | Java Tutorials | 0 | 01-11-2007 03:05 PM |