Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
JDBC 3

JDBC 3

Ratings: (0)|Views: 51 |Likes:
Published by api-3749401

More info:

Published by: api-3749401 on Oct 16, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

DataBaseMetaData Class
An Example
import java.util.*;
import java.sql.*;
class Jdbc

{
public static void main(String[] args)
{

try

{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c =

DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
DatabaseMetaData dm = c.getMetaData();
System.out.println("Database Name "+dm.getDatabaseProductName());

System.out.println("Database Version

"+dm.getDatabaseProductVersion());
System.out.println("Database URL "+dm.getURL());
System.out.println("User Name "+dm.getUserName());
System.out.println("Database Driver " + dm.getDriverName());
System.out.println("Database Driver Version "+dm.getDriverVersion());
System.out.println(" Major Version "+dm.getDriverMajorVersion());

ResultSet rs = dm.getTables(null,null,null,null);
while(rs.next())
{System.out.println(rs.getString(3));
}c.close();
}catch(Exception e)
{
System.out.println(e);
}
}
Example of using the setLogWriter method
import java.sql.*;
import java.io.*;
class adjdbc1o
{
public static void main(String[] args)
{
try

{
DriverManager.setLogWriter(new PrintWriter(System.out));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection

c=DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
Statement st=c.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
while(rs.next())
{

for(int i=1;i<5;i++)
{
System.out.print(rs.getString(i)+".");
}
System.out.println();
}
}catch(Exception e)
{
e.printStackTrace();
}
} // main ends
} //class ends
Another example of Prepared Statement for Data input.

// example of prepared statement.
import java.sql.*;
class Jdbc1
{

public static void main(String[] args)
{
try
{
DriverManager.registerDriver(new
sun.jdbc.odbc.JdbcOdbcDriver());
Connection
c=DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger");
PreparedStatement ps =c.prepareStatement("update deptmaster set
loc=? where deptno=?");
ps.setString(1,"Germany");
ps.setInt(2,14);
int i = ps.executeUpdate();
if (i ==0)
{
System.out.println("No records updated");
}else if(i ==1)
{
System.out.println("One record updated");
}ps.setString(1,"MumbAI");

ps.setInt(2,10);
i = 0;
i = ps.executeUpdate();

if (i ==0)
{
System.out.println("No records updated");
}else if(i ==1)
{
System.out.println("One record updated");
}ps.close();
c.close();
}catch(Exception e)
{
e.printStackTrace();
}
} // end of main
} // end of class
New Features in JDBC 2.0 API
a) Scrollable Result set
b) Using java methods (executeUpdate ()) for insert, delete and no need for
remembering SQL syntax.
c) Batch Updates- This will ensure that a batch goes at one time to the backend,
perform the action and then come back.
TYPE_FORWARD_ONLY- This is the default setting in a normal RS

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->