Thursday, July 14, 2016

Write a program by using JDBC to execute a SQL Query for a database and display the results.

Aim: Write a program by using JDBC to execute a SQL Query for a database and display the results.


Program:

import java.sql.*;
import java.util.*;
class Idata
{
public static void main(String args[])
{
try
{

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","sumanth");
Statement st= c.createStatement();
int r=st.executeUpdate("insert into student36(num,name) values(11,'swap')");
System.out.println("\n\n1 Row inserted\n\n");

st= c.createStatement();
ResultSet r1=st.executeQuery("select *from student36");

while(r1.next())
{

int x = r1.getInt("NUM");
String y= r1.getString("NAME");

System.out.println(x+"\t "+y);
}


}
catch(Exception e)
{
}
}
}

Result:

F:\ajprograms\5>javac Idata.java

F:\ajprograms\5>java Idata

1 Row inserted

10       sumanth
11       swap
12       honey
13       diyashirley
11       swap


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home