Monday, September 13, 2010

PreparedStatement in java with select


PreparedStatement in java with select sql statement:
Here listed below code for select the records using PreparedStatementin java

<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.*" %>

String userName = "root",password = "",url = "jdbc:mysql://localhost:3306/test",driver = "com.mysql.jdbc.Driver";
Connection conn=null;
try {
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url, userName,password);
}catch(Exception ee) {
    out.print("There are some problems");
    out.print(ee.toString());
}
String sqlAdminTypeList="select *from admin_type order by admin_type_text";
PreparedStatement psAdminTypeList= null;
psAdminTypeList = conn.prepareStatement(sqlAdminTypeList);
ResultSet rsAdminTypeList=psAdminTypeList.executeQuery();
StringBuilder sbAdminTypeList=new StringBuilder();
while(rsAdminTypeList.next()) {
sbAdminTypeList.append("<option value=\""+rsAdminTypeList.getInt("admin_type_id")+"\">"+rsAdminTypeList.getString("admin_type_text")+"</option>");
}
rsAdminTypeList.close();
rsAdminTypeList=null;

No comments:

Post a Comment