JDBC – Read details using BufferedReader and Insert

Insert Record by reading account details using BufferedReader:

  • BufferedReader class belongs to IO package.
  • BufferedReader class is providing readLine() method by which we can read input values.
  • readLine() method returns input in String format only.
  • We need to perform String to other type conversions if required.
package online;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class PreparedInsertRecord
            static Connection con;
            static BufferedReader br;
            public static void main(String args[]) throws Exception
                                    System.out.println(“Driver is ready”);
                                    con = DriverManager.getConnection(” jdbc:oracle:thin:@localhost:1521:xe”, “system”, “admin”);
                                    System.out.println(“Connection is ready”);
                                    br = new BufferedReader(new InputStreamReader(System.in));
                                    System.out.println(“Enter account details(num, name, balance : “);
                                    int nu = Integer.parseInt(br.readLine());
                                    String na = br.readLine();
                                    int ba = Integer.parseInt(br.readLine());
                                    String query = “insert into account values(?,?,?)”;
                                    PreparedStatement stmt=con.prepareStatement(query);
                                    stmt.setInt(1, nu);
                                    stmt.setString(2, na);
                                    stmt.setInt(3, ba);
                                    int count = stmt.executeUpdate();
                                    System.out.println(“Updated record(s) : ” + count);                            
                                    if(con != null)
                                                System.out.println(“Connection closed”);
                                    if(br != null)
                                                System.out.println(“Buffer closed”);

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top