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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jiteendra SampathiraoRank #585 · 4.1K reputationPosted Sep 4, 2011, 8:48 PM EDT
refer this link and find the pdf in download link
Prabhu RajaRank #861 · 2.6K reputationPosted Sep 4, 2011, 9:04 PM EDT
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
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 4, 2011, 9:57 PM EDT
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
AartiRank #2,383 · 594 reputationPosted Nov 16, 2011, 11:16 PM EST
Prabhu RajaRank #861 · 2.6K reputationPosted Nov 16, 2011, 11:51 PM EST
i think it was too late to answer this post . Look at the Date of Thread was posted.
AartiRank #2,383 · 594 reputationPosted Nov 17, 2011, 12:03 AM EST
Yes i saw the post date of thread. and at all :)