Create an application which can demonstrate the use of JDBC for Database Connectivity

Program: -
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;

public class Preparedstmnt {
public static void main(String[] args) throws ClassNotFoundException,SQLException, IOException {      
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection cn=DriverManager.getConnection("jdbc:odbc:XE");
        String query="insert into mytable values(?,?,?)";
        PreparedStatement ps=cn.prepareStatement(query);
        ps.setString(1,"pqr1");
        ps.setInt(2,45);
        ps.setDouble(3, 145.50);
        int i=ps.executeUpdate();
        System.out.println("Data Inserted...");       
        ps.setString(1,"abc1");
        ps.setInt(2,85);
        ps.setDouble(3, 195.50);
        i=ps.executeUpdate();
        System.out.println("Data Inserted...");       
        System.out.println("Your Inserted Data Are...");
        ps=cn.prepareStatement("select * from mytable");   
        ResultSet res=ps.executeQuery();
        while(res.next()){
            System.out.println("Value 1 : " + res.getString(1));
            System.out.println("Value 2 : " + res.getInt(2));
            System.out.println("Value 3 : " + res.getDouble(3));           
        }      
       cn.close();
    }


Output



Post a Comment

0 Comments