From Twitter

USING JDBC TO QUERY A DATABASE

USING JDBC TO QUERY A DATABASE

Let us take an example to understand how to query or modify a database. Consider a
table named as CUSTOMER is created in MS-ACCESS, with fields cust_id, name,
ph_no, address etc.

import java.sql.*;
 
public class JdbcExample1 {
 
public static void main(String args[]) {
 
Connection con = null;
 
Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);
 
Connection Conn = DriverManager.getConnection (jdbc:odbc:Access);
 
Statement Stmt = Conn.createStatement();
 
// To create a string of SQL.
 
String sql = "SELECT * FROM CUSTOMERS";
 
// Next we will attempt to send the SQL command to the database.
 
// If it works, the database will return to us a set of results that JDBC will
 
// store in a ResultSet object

try
 
{
 
ResultSet results = Stmt.executeQuery(sql);
 
// We simply go through the ResultSet object one element at a time and print //out the
fields. In this example, we assume that the result set will contain three //fields
 
while (results.next())

System.our.println("Field One: " + results.getString(1) + "Field Two: " +
results.getString(2) + "Field Three: " + results.getString(3));
}
}
// If there was a problem sending the SQL, we will get this error.
catch (Exception e)
{
System.out.println("Problem with Sending Query: " + e);
}
finally
{
result.close();
stmt.close();
Conn.close();
}
} // end of main method
} // end of class

Note that if the field is an Integer, you should use the getInt() method in ResultSet
instead of getString().You can use either an ordinal position (as shown in the above
example) which starts from 1 for the first field or name of field to access the values
from the ResultSet like result.getString(“CustomerID”);
 

Compiling JdbcExample1.java


To compile the JdbcExample1.java program to its class file and place it according to
its package statements, open command prompt and cd (change directory) into the
folder containing JdbcExample2.java, then execute this command:
 

javac -d . JdbcExample2.java


If the program gets compiled successfully then you should get a new Java class under
the current folder with a directory structure JdbcExample2.class in your current
working directory.
 

Running JdbcExample1.java


To run and to see the output of this program execute following command from the
command prompt from the same folder where JdbcExample2.java is residing:
 

java JdbcExample2

No comments:

Post a Comment

Labels

(MCS-031 (6) 2011 (5) 4nf (1) 5nf (1) ACCESS CONTROL In Relational Database (1) ALGORITHMICS (5) assignment 2014 2015 (1) AVAILABLE TOOLS & ALGORITHMS (5) BCA (1) BINARY SEARCH (1) Block Nested Loop Join (1) Build and Fix Model (1) BUILDING BLOCKS OF ALGORITHMS (1) CHARACTERISTICS OF AN ALGORITHM (2) Core Java (1) Data Communication Network Security (1) DATABASE SECURITY (1) EER tool (1) ELEMEMTARY ALGORITHMICS (2) ENHANCED ER TOOLS (1) EVOLUTION (1) EXAMPLE OF AN ALGORITHM (2) Indexed Nested-Loop Join (1) install servelet engine (1) INTRODUCTION (1) Iterative Enhancement Model (1) Java Server Pages (1) JDBC (1) JSP (2) LEVELS OF DATABASE SECURITY (1) MCA (9) MCA 051 (1) MCA 3rd Semester (8) MCA 4th Semester (1) MCA 5 sem (1) MCS-031 (7) MCS-031 : DESIGN AND ANALYSIS OF ALGORITHM (14) MCS-032 (1) MCS-033 (1) MCS-034 (2) MCS-035 (1) mcs-041 (2) MCS-042 (1) mcs-043 (2) mcs-052 solved assignment (1) MCSL-036 (2) Nested loop join (1) OBJECTIVES (1) Operating System (2) OUTLINE OF ALGORITHMICS (1) Principles of Management and Information Systems (1) PROBLEMS (1) QUERY PROCESSING AND EVALUATION (1) Query processing Optimisation (1) Question Papers (8) Related Topic (9) relational Database (1) SELECT OPERATION Query Processing (1) Servlet (1) Servlet Programme (1) Servlet Programming (1) SOFTWARE DEVELOPMENT MODELS (4) SOFTWARE ENGINEERING (4) Solution (7) Solved Assignment 2013 2014 (6) SOME PRE-REQUISITES AND Asymptotic Bounds ASYMPTOTIC BOUNDS INTRODUCTION (1) STATISTICAL DATABASE SECURITY (1) structure (1) SUMMARY (1) Waterfall Model (1) Write a C program to print the following triangle (1)