Hi
Can anyone tell me the exact procedure for the JDBC Connectivity in JAVA with an example?
Thanks in Advance
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AartiPosted Nov 17, 2011, 5:03 AM
Yes i saw the post date of thread. and at all :)
Prabhu RajaPosted Nov 17, 2011, 4:51 AM
i think it was too late to answer this post . Look at the Date of Thread was posted.
AartiPosted Nov 17, 2011, 4:16 AM
Satyapriya NayakPosted Sep 5, 2011, 1:57 AM
These are the steps to connecting to the database
1:Loading the drivers
2:Get the connection
3:Create the statement
4:Execute the query
5:Process the resultset
6:Close the connection
Creation of dsn(database source name)
Start-Control panel- Administrative Tools- Data Sources (ODBC)-go to system dsn tab-click add button-select a driver for which you want to set up data source (for Oracle- Oracle in XE)-select it and click finish-give any name in data source name textbox-then click ok button.
Ex:-
//Display records from the database using servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class display extends HttpServlet
{
public void doGet(HttpServletRequest req ,HttpServletResponse res)throws IOException ,ServletException
{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","system","pintu");
// Here dsnname- mydsn,user id- system(for oracle 10g),password for this is pintu
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee");
out.println("
out.println("
while(rs.next())
{
String n=rs.getString("empid");
String nm=rs.getString("empname");
int s=rs.getInt("sal");
out.println("
}
out.println("
out.println("");
con.close();
}
catch(Exception e)
{
out.println("error");
}
}
}
Thanks
If this post helps you mark it as answer
Prabhu RajaPosted Sep 5, 2011, 1:04 AM
import java.sql.* ;
class JDBCQuery
{
public static void main( String args[] )
{
try
{
// Load the database driver
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
// Get a connection to the database, Here Database is DataSource Name(DSN)
//Username and password is optional
Connection conn = DriverManager.getConnection( "jdbc:odbc:Database",Username,Password ) ;
// Print all warnings
for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
{
System.out.println( "SQL Warning:" ) ;
System.out.println( "State : " + warn.getSQLState() ) ;
System.out.println( "Message: " + warn.getMessage() ) ;
System.out.println( "Error : " + warn.getErrorCode() ) ;
}
// Get a statement from the connection
Statement stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( "SELECT * FROM Cust" ) ;
// Loop through the result set
while( rs.next() )
System.out.println( rs.getString(1) ) ;
// Close the result set, statement and the connection
rs.close() ;
stmt.close() ;
conn.close() ;
}
catch( SQLException se )
{
System.out.println( "SQL Exception:" ) ;
// Loop through the SQL Exceptions
while( se != null )
{
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
}
}
catch( Exception e )
{
System.out.println( e ) ;
}
}
}
Is this helps you, then click "This is Correct answer" CheckBox
Jiteendra SampathiraoPosted Sep 5, 2011, 12:48 AM
refer this link and find the pdf in download link