Java J2ee Tutorials

Insert Record Using JSP MYSQL

Saturday, January 14, 2012

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "java.sql.*"%>
Index
<%
int emp_id = Integer.parseInt(request.getParameter("employeeID"));
String str2 = request.getParameter("employeeName");
String str_email = request.getParameter("employeeEmail");
Connection con = null;
PreparedStatement psmt = null;
String url = "jdbc:mysql://localhost:3306/test_db";
String driver = "com.mysql.jdbc.Driver";
//String dbName = "test_db";
String userName = "root";
String password = "";
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url, userName, password);
//con = DriverManager.getConnection(url+dbName, userName, password);
String sql = "INSERT INTO EMP_DETAIL(emp_id, emp_name, emp_email) VALUES (?, ?, ?)";
psmt = con.prepareStatement(sql);
psmt.setInt(1, emp_id);
psmt.setString(2, str2);
psmt.setString(3, str_email);
psmt.executeUpdate();
out.println("Thank you: Record inserted.");
}
catch (Exception e){
out.println("Error: "+e);
}
finally {
psmt.close();
con.close();
}
%>

Free Career Predictions

0 comments:

Post a Comment