pallavi
pallavi

Reputation: 13

Connecting to a Database with Java

I'm very confused about how to connect my Java application to a database, any database. I'm not sure where to start. Could anybody provide for me a simple example that might help me along?

Upvotes: 0

Views: 576

Answers (3)

snk
snk

Reputation: 1

To connect to SQL Server from a Java application, you need to use the JDBC API. The JDBC API provides classes and methods that connect to the database, load the appropriate driver, send SQL queries, retrieve results etc.

HOW TO CONNECT TO THE DATABASE

A Connection object represents a connection with a database. To establish the connection, use the method DriverManager.getConnection. This method takes a string containing a URL which represents the database we are trying to connect to. Below is the sample code for establishing a connection:

private String DATABASE_URL = "jdbc:odbc:embedded_sql_app";
// establish connection to database
Connection connection = DriverManager.getConnection( DATABASE_URL,"sa","123" ); 

Detailed discussion about the Database URL and how to create it can be found in the resource provided at the end of this post.

QUERYING THE DATABASE

The JDBC API provides three interfaces for sending SQL statements to the database, and corresponding methods in the Connection interface create instances of them.

  1. Statement - created by the Connection.createStatement methods. A Statement object is used for sending SQL statements with no parameters.
  2. PreparedStatement - created by the Connection.prepareStatement methods. A PreparedStatement object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
  3. CallableStatement - created by the Connection.prepareCall methods. CallableStatement objects are used to execute SQL stored procedures from Java database applications.

RETRIEVING THE RESULT

A ResultSet is a Java object that contains the results of executing a SQL query. The data stored in a ResultSet object is retrieved through a set of get methods that allows access to the various columns of the current row. The ResultSet.next method is used to move to the next row of the ResultSet, making it the current row.

The following code fragment executes a query that returns a collection of rows, with column a as an int, column b as a String, and column c as a float:

java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) {
    // retrieve and print the values for the current row
    int i = rs.getInt("a");
    String s = rs.getString("b");
    float f = rs.getFloat("c");
    System.out.println("ROW = " + i + " " + s + " " + f);
}

This is just a brief introduction on how to interact with a database from Java. For more details on the items discussed above as well as information on passing parameters, executing stored procedures etc. please refer to the following resource:

http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html#Shahriar_N_Embedding_SQL_in_Java

Here, you will also find information on how to interact with a database programmatically; i.e. without using SQL.

Hope you find this useful.

Upvotes: 0

magallanes
magallanes

Reputation: 6854

Aside of jdbc, You can connect using ibatis (or mybatis) and Hibernate. Ibatis is quite simple (but sometimes tricky to configure) but it do the trick.ibatis example

Upvotes: 0

nos
nos

Reputation: 229342

You're welcome to ask more specific questions, but as the question is quite general, start with the JDBC tutorial from Oracle, http://download.oracle.com/javase/tutorial/jdbc/index.html

Upvotes: 2

Related Questions