Saturday, March 28, 2009

JDBC Example-Statement Object

Detailed JDBC Example-Statement Object:-

The simple quries that include retrieving data from database comes under statement object.As told in previous tutorials statement object is used for very basic database function that is getting data from database.

Following example include Simple functions which will be explained further lets take a look at example:-

import java.sql.*;

public class jdbcexample
{
static Connection conn;
static Statement stat;
static ResultSet result;
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:Mydata");
stat=conn.createStatement();
result=stat.executeQuery("select * from emp");
while(result.next())
{
System.out.println(result.getString(1));
System.out.println(result.getString(2));
System.out.println(result.getString(3));
}
}catch(Exception e){}
}
}

This is very basic exapmle here all variable before main method are declared static because all these variables are used in main method which is static and you cannot use any non static variable in static methods .So you have to declare them static.
Coming to point as you know statement object is used to get value from database only not making any structural changes.For making structural changes you have to use preparedStatement object.In statement object first you have to create the statement using create Statement object of Connection class after that result is obtained in ResultSet object by using method executeQuery(String s) method of Statement object.After that continue to fetch data .

Go to :-
Home
JDBC Architecture
Configuring ODBC Sources
Connecting to database
Querying Database
Using ResultSet Object

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP