PreparedStatement with insert in java
Here listed below code for insert 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.*" %>
java.util.Date now = new java.util.Date();
String addMsg="";
String admin_login_add=request.getParameter("admin_login_add");
String admin_pass_add=null;
int admin_type_id_add=0;
if(admin_login_add!=null) {
admin_login_add=admin_login_add.trim();
admin_pass_add=request.getParameter("admin_pass_add");
admin_type_id_add=Integer.parseInt(request.getParameter("admin_type_id_add"));
try {
PreparedStatement psAdminAdd= null;
String sqlAdminAdd="Insert into admin_info (admin_login,admin_pass,admin_type_id) values(?,?,?)";
psAdminAdd = conn.prepareStatement(sqlAdminAdd);
psAdminAdd.setString(1,admin_login_add);
psAdminAdd.setString(2,admin_pass_add);
psAdminAdd.setInt(3,admin_type_id_add);
psAdminAdd.executeUpdate();
psAdminAdd.close();
psAdminAdd=null;
addMsg="<font color='green'><b>Admin user added.</b></font>";
} catch (java.sql.SQLException e) {
if(e.getErrorCode()==1062 && Integer.parseInt(e.getSQLState())==23000) {
addMsg="<font color='red'><b>This user already exists.</b></font>";
}
}
}
Here listed below code for insert 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.*" %>
java.util.Date now = new java.util.Date();
String addMsg="";
String admin_login_add=request.getParameter("admin_login_add");
String admin_pass_add=null;
int admin_type_id_add=0;
if(admin_login_add!=null) {
admin_login_add=admin_login_add.trim();
admin_pass_add=request.getParameter("admin_pass_add");
admin_type_id_add=Integer.parseInt(request.getParameter("admin_type_id_add"));
try {
PreparedStatement psAdminAdd= null;
String sqlAdminAdd="Insert into admin_info (admin_login,admin_pass,admin_type_id) values(?,?,?)";
psAdminAdd = conn.prepareStatement(sqlAdminAdd);
psAdminAdd.setString(1,admin_login_add);
psAdminAdd.setString(2,admin_pass_add);
psAdminAdd.setInt(3,admin_type_id_add);
psAdminAdd.executeUpdate();
psAdminAdd.close();
psAdminAdd=null;
addMsg="<font color='green'><b>Admin user added.</b></font>";
} catch (java.sql.SQLException e) {
if(e.getErrorCode()==1062 && Integer.parseInt(e.getSQLState())==23000) {
addMsg="<font color='red'><b>This user already exists.</b></font>";
}
}
}
No comments:
Post a Comment