0% found this document useful (0 votes)
44 views4 pages

SV WT Record Week 11

The document describes three Java programs that access a database: 1. dbcreate.java creates a database table 2. dbupdate.java updates a record in the table 3. dbresult.java retrieves and displays records from the table The programs demonstrate how to connect to a database, execute SQL statements, and retrieve result sets using JDBC.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

SV WT Record Week 11

The document describes three Java programs that access a database: 1. dbcreate.java creates a database table 2. dbupdate.java updates a record in the table 3. dbresult.java retrieves and displays records from the table The programs demonstrate how to connect to a database, execute SQL statements, and retrieve result sets using JDBC.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exp No:

11

Date:

SRI VENKATESWARA COLLEGE OF ENGINEERING


Karakambadi Road, Tirupathi. -517507
Department of Computer Science and Engineering
M.Tech I Semester
Web Technologies Lab

Title: database accessing

Exp. No: 11

Aim: Explain java program that used to access database


Procedure:
1. Open Notepad.
2. Select New Option |
3. Write the html code
4. go to File Menu | Select Save option. Choose directory as d:\html\week1\
5. Give the name as home.html
6. The logo.html file can be represented as web page.
7. Go to d:\html\week1\ directory and double click on home.html
8. The Result will be display on Interet Explorer.
|----------------------------------------- |
|Code: File name save ::dbcreate.java |
|------------------------------------------|
import java.sql.* ;
class dbcreate
{
public static void main( String args[] )
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
//Connection conn =
DriverManager.getConnection("jdbc:odbc:mydsn"); //For MSAccess connection
Connection conn =
DriverManager.getConnection("jdbc:odbc:oracle","scott","tiger"); //For Oracle connection
Statement stmt = conn.createStatement();
stmt.executeUpdate("create table books("bookisdn number(2),bookname
varchar2(15),price number(10),quantity number(3) amount number(10))");
stmt.close() ;

SRI VENKATESWARA COLLEGE OF ENGINEERING, TIRUPATI-517507

Exp No:

11

Date:
conn.close() ;
}
catch(Exception e)
{
System.out.print("ERROR");
}

}
}

|----------------------------------------- |
|Code: File name save ::dbupdate.java |
|------------------------------------------|
import java.sql.* ;
class dbupdate
{
public static void main( String args[] )
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
Connection conn = DriverManager.getConnection("jdbc:odbc:mydsn") ;
Statement stmt = conn.createStatement();
stmt.executeUpdate("update books set price=419 where bookisbn=12");

SRI VENKATESWARA COLLEGE OF ENGINEERING, TIRUPATI-517507

Exp No:

11

Date:
stmt.close() ;
conn.close() ;
}
catch(Exception e)
{
System.out.print("ERROR");
}

}
}
Result:

|----------------------------------------- |
|Code: File name save ::dbresult.java |
|------------------------------------------|
import java.sql.* ;
class dbresult
{
public static void main( String args[] )
{
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;

SRI VENKATESWARA COLLEGE OF ENGINEERING, TIRUPATI-517507

Exp No:

11

Date:
Connection conn = DriverManager.getConnection( "jdbc:odbc:mydsn" ) ;
Statement stmt = conn.createStatement() ;
ResultSet rs = stmt.executeQuery( "SELECT * FROM books" ) ;
System.out.println("BookISBN:Book Name:Price:Quantity:Amount");
while( rs.next() )
{

System.out.println("\n"+rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.
getString(4)+"\t"+rs.getString(5));
}
rs.close() ;
stmt.close() ;
conn.close() ;
}
catch(Exception e)
{
System.out.print("ERROR");
}
}
}
Result:

Conclusion: The above programs are successfully to access database.

SRI VENKATESWARA COLLEGE OF ENGINEERING, TIRUPATI-517507

You might also like