Accessing Databases Using Java and JDBC

This article will show how a Java Application can access database and then list tables in it.

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

Accessing Database using Java and JDBC

     

Accessing Database using Java and JDBC

Database plays an important role in storing large amount of data in a pattern. Here we are going develop and example to access the database using Java and JDBC. For this, firstly we need to establish a connection between database and java file with the help of various types of APIs, interfaces and methods. We are using MySQL database.

Connection: This  interface specifies connection with specific databases like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of this interface.

Class.forName(String driver): It loads the driver.

DriverManager: This class controls a set of JDBC drivers. Each driver has to be register with this class.

getConnection(String url, String userName, String password): This method establishes a connection to specified database url. It is having three arguments:
  url: - Database url where stored or created your database
  username: - User name of MySQL
  password: -Password of MySQL 

Here is the video tutorial of accessing the database from Java program:

getMetaData(): This is a method of Connection interface. It retrieves the metadata of the database.

DataBaseMetaData: This interface gives information about the database like number of tables in the database, columns of the table etc.

getTables(null, null, "%", null): This method provides the description of the tables available in the given catalog. As we have set other parameters null, so it will provide only table names.

Here is the code:

import java.sql.*;

public class AccessDatabases {
  public static void main(String[] args) {
  try {
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection(
  "jdbc:mysql://localhost:3306/test""root""root");
  Statement st = con.createStatement();
  DatabaseMetaData meta = con.getMetaData();
  ResultSet rs = meta.getTables(null, null, "%"null);
  String tableNames = "";
  while (rs.next()) {
  tableNames = rs.getString(3);
  System.out.println(tableNames);
  }
  catch (Exception e) {
  }
  }
}

In this section we studies how to connect to database and then list all the tables in the database. We have used MySQL database server.

Next: Check more tutorials of JDBC Programming using MySQL.

Advertisements

Share on Google+Share on Google+

Accessing Databases Using Java and JDBC

Posted on: August 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Accessing Databases Using Java and JDBC  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
Anand
April 13, 2011
jdbc

what is the meaning of "jdbc:mysql://localhost:3306/test", "root", "root")" and how to upload the to the server?
Anand
April 13, 2011
oracle

I want access data from oracle(Window XP)? my DSN is "DEMO". My table name is "student". I am using servlet program,then tell me JDBC connection program for servlet.
shobana
June 23, 2011
java

Its very useful for freshers to develop our programming knowledge. for now i need the procedure of how to connect netbeans with mysql.if any one know that procedure please tell me.
shorinyana
December 27, 2011
Thanks this Tutorial

I am very happy for this Tutorial which help me to understand more about how to access a database using Java and JDBC. I need more Tutorial for how to build a Web Application using Java. Thank You.
yamini
March 15, 2012
jdbc

how to access database using java and jdbc

Ads

 

Ads