1.First I created the HTML form with two fields with an action that sends the data to the "AddUserDbd.jsp" ...
2. "AddUserDbd.jsp" initialize the connection and two functions "OpenConnection" and "InsertUsers(,,)"
3. getParameter is used to read the fields from ny HTML form e.g
Name = request.getParameter("userName"); Password = request.getParameter("passWord");
and both Paramenters are then iserted into the function InsertUsers(Name,Password);
see the code to understand ..
-------------------- HTML form -------------------------------------
-----------------------------------------------------------------------------------
AddUserDbd.jsp ----------- Processing File --------------
<%@ page import = "java.sql.*"%>
<%! String url ="jdbc:odbc:monthly";
Connection conn;
Statement stmt;
String Name,Password = "";
int Level;
String SQL;
public void OpenConnection()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{
System.out.println(e.toString());
}
try
{
conn = DriverManager.getConnection(url,"sa","");
}
catch(Exception e)
{
System.out.println("Could not load Driver");
}
} // end Openconnection
public void InsertUsers(String UserName,String Passwd,int Level)
{
try
{
stmt = conn.createStatement(); //connect the statement to the connection
SQL ="insert into Access(UserName,Password,AdminLevel) values('" + UserName + "','" + Passwd + "'," + Level + ")";
stmt.executeUpdate(SQL);
}
catch(SQLException ee)
{
System.out.println(ee);
}
} //end InsertUsers
%>
<%
OpenConnection();
Name = request.getParameter("userName");
Password = request.getParameter("passWord");
InsertUsers(Name,Password,1);
out.println(SQL);
conn.close();
stmt.close();
%>
Place AddUserDbd.jsp's content here
No comments:
Post a Comment