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:
Post a Comment
Post a Comment