//JDBC Program insert record and display record
import java.sql.*;
public class student
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
Connection con = DriverManager.getConnection("jdbc:odbc:TYBCA"); //Create Connection with Data Source Name : BCAII
Statement stmt = con.createStatement(); // Create Statement
stmt.executeUpdate("insert into student values(1,'sachin','bmt')");
// stmt.executeQuery("select * from student"); // Execute Query
ResultSet rs = stmt.executeQuery("select * from student"); //return the data from Statement into ResultSet
while(rs.next()) // Retrieve data from ResultSet
{
System.out.println("Roll no : "+rs.getInt(1));//1st column of Table
System.out.println("Name : "+rs.getString(2)); //2st column of Table from database
System.out.println("Address : "+rs.getString(3)); //3rd column of Table
}
stmt.close();
con.close();
}
catch (Exception e)
{
System.out.println("Exception : "+e);
}
}
}
/*Output
D:\sachin>javac student.java
D:\sachin>java student
Roll no : 1
Name : sachin
Address : bmt
*/
import java.sql.*;
public class student
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
Connection con = DriverManager.getConnection("jdbc:odbc:TYBCA"); //Create Connection with Data Source Name : BCAII
Statement stmt = con.createStatement(); // Create Statement
stmt.executeUpdate("insert into student values(1,'sachin','bmt')");
// stmt.executeQuery("select * from student"); // Execute Query
ResultSet rs = stmt.executeQuery("select * from student"); //return the data from Statement into ResultSet
while(rs.next()) // Retrieve data from ResultSet
{
System.out.println("Roll no : "+rs.getInt(1));//1st column of Table
System.out.println("Name : "+rs.getString(2)); //2st column of Table from database
System.out.println("Address : "+rs.getString(3)); //3rd column of Table
}
stmt.close();
con.close();
}
catch (Exception e)
{
System.out.println("Exception : "+e);
}
}
}
/*Output
D:\sachin>javac student.java
D:\sachin>java student
Roll no : 1
Name : sachin
Address : bmt
*/