8/17/10

Creating views using mysql

Creating view in java for mysql is simple.Lets see an example.



import java.sql.*;


public class ViewExample {




    public static void createView() {
        Connection con = null;
        Statement stat = null;
        Statement stat1 = null;
        Statement stat2 = null;
        ResultSet rs = null;
        long now = 0;
        try {






            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
       


            stat = con.createStatement();


            int b;
            stat.executeUpdate("drop view IF EXISTS test.view1");
            stat.executeUpdate("CREATE VIEW test.view1(first_name,last_name) AS SELECT s_26_field_13,s_26_field_12 FROM S_26");


            stat1 = con.createStatement();


            System.out.println("view created ");


            stat2 = con.createStatement();
            rs = stat.executeQuery("select * from view1");
            while (rs.next()) {
                System.out.println("first_name -- " + rs.getString("first_name"));
                System.out.println("last_name -- " + rs.getString("last_name"));
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String a[]) {
       
        createView();


    }
}

No comments:

Post a Comment

Popular Posts