Tuesday, September 14, 2010

PreparedStatement in java with update

PreparedStatement in java with update sql statement:
Here listed below code for update the record using PreparedStatementin java


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ 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";
String siteUrl="http://localhost:8080/demo/";
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());
Systen.exit(0);
}

String acDecMsg="";
String sqlAdminActDea=null;
if(request.getParameter("admin_id")!=null && request.getParameter("stat")!=null) {
    int d_admin_id=Integer.parseInt(request.getParameter("admin_id").toString());
    int stat=Integer.parseInt(request.getParameter("stat").toString());
    if(d_admin_id!=1) {
        PreparedStatement psAdminActDea= null;
        sqlAdminActDea="Update admin_info set active_stat=? , update_date=? where admin_id=?";
        psAdminActDea = conn.prepareStatement(sqlAdminActDea);
        psAdminActDea.setInt(1,stat);
        psAdminActDea.setTimestamp(2, new java.sql.Timestamp( now.getTime()) );
        psAdminActDea.setInt(3,d_admin_id);
        if(psAdminActDea.executeUpdate()>0) {
            acDecMsg=stat>0?"<font color='green'><b>Record activated.</b></font>":"<font color='green'><b>Record deactivated.</b></font>";
        } else {
            acDecMsg="<font color='red'><b>This record not found.</b></font>";
        }
        psAdminActDea.close();
        psAdminActDea=null;
    } else {
        acDecMsg="<font color='red'><b>You can not make active/deactive your self.</b></font>";
    }
}

No comments:

Post a Comment