0% found this document useful (0 votes)
527 views2 pages

AJP No.18

This document contains code to insert and retrieve data from a database using JDBC. The first code sample inserts a new record into a Student table. The second code sample retrieves all records from the Student1 table and prints the results. It loads the JDBC driver, connects to the database, creates a statement object, executes an insert or select query, and prints the output.

Uploaded by

Saraswati Shelke
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)
527 views2 pages

AJP No.18

This document contains code to insert and retrieve data from a database using JDBC. The first code sample inserts a new record into a Student table. The second code sample retrieves all records from the Student1 table and prints the results. It loads the JDBC driver, connects to the database, creates a statement object, executes an insert or select query, and prints the output.

Uploaded by

Saraswati Shelke
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/ 2

Practical No: 18

Title: Write a program to insert and retrieve data from database using JDBC.

//Program to insert value into table


import java.sql.*;
class InsertP
{
public static void main(String ar[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers loaded");
Connection c=DriverManager.getConnection("jdbc:odbc:Stud1");
System.out.println("Connection Created");
Statement st=c.createStatement();
System.out.println("Statement Object Created");
st.executeUpdate("insert into Student values(2,'lingayat',95)");
c.commit();
c.close();
}
}
OUTPUT:
C:\jdk1.7.0\bin>java InsertP
Drivers loaded
Connection Created
Statement Object Created
---------------------------------------------------------------------------------------------------

//Program to retrive values


import java.sql.*;
class Pr18Display
{
public static void main(String ar[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drivers loaded");
Connection c=DriverManager.getConnection("jdbc:odbc:Stud1");
System.out.println("Connection Created");
Statement st=c.createStatement();
System.out.println("Statement Object Created");
ResultSet rs=st.executeQuery("Select * from student1");
String msg="";
System.out.println("RollNo\tName\tMarks\tDepartment");
while(rs.next())
{
msg=msg+rs.getInt(1)+"\t"+rs.getString(2)+"\
t"+rs.getInt(3)+"\t"+rs.getString(4)+"\n ";
}
System.out.println(msg);
}
}
OUTPUT:
Drivers loaded
Connection Created
Statement Object Created
RollNo Name Marks Department
1 preeti 85 cm
2 aditi 94 if
3 mayuri 90 civil
4 anjali 89 cm

You might also like