JAVA N WEB TECH SYLLABUS

Wednesday, December 22, 2010

Using Prepared Statement

In this code we r makin use of the PreparedStatement for displayin a particular student record from student table


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/*
 * Display.java
 *
 * Created on December 14, 2010, 9:25 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Naveen
 */
public class Display {
  
    /** Creates a new instance of Display */
    public Display() {
    }
    public static void main(String args[]) {
        PreparedStatement pstmt=null;
        Connection conn = null;
        ResultSet rs=null;
      
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        String url="jdbc:mysql://localhost/studentinfo";
        String userName="root";
        String password="root";
        try {
          
            conn=DriverManager.getConnection(url,userName,password);
          
        }
      
        catch (SQLException ex) {
            System.out.println("Unable to Connect to Database");
        }
      
        try {
          
            String query="select * from student where cgpa=? and sem =?";
            pstmt=conn.prepareStatement(query);
          
            pstmt.setFloat(1,8.75f);
            pstmt.setInt(2,7);
            rs =pstmt.executeQuery();
            while(rs.next()) {
                System.out.print(rs.getString(1)+"\t");
                System.out.print(rs.getString(2)+"\t");
                System.out.print(rs.getString(3)+"\t");
              
                System.out.print(rs.getInt(4)+"\t");
                System.out.print(rs.getFloat(5)+"\t");
            }
          
          
        } catch (SQLException ex) {
            System.out.println("Unable to create Statment");
        }
      
      
      
      
      
    }
}

1 comment:

  1. Borgata Hotel Casino & Spa - Mapyro
    Borgata Hotel Casino 상주 출장샵 & Spa is an MGM 서산 출장샵 Resorts 보령 출장샵 hotel in Atlantic City, New Jersey. The property is a 나주 출장안마 16-story 청주 출장마사지 high-rise building in

    ReplyDelete