Preparatory work:
(1) Creating and using the database: Create databases student; Use test;
(2) Creating table: Create TABLE PPP (Sno char (7), sname char (8));
(3) Insert a piece of data: INSERT into PPP values (123,456);
(4) Display data: Select*from PPP; (Review database knowledge)
Begin:
(1) Open myeclipse. Create a new Web project. (Mysql) and build a second.jsp in Webroot
(2) Enter the code in INDEX.JSP:
<body>
<form action= "second.jsp", method= "get" >
User name: <input type= "text" name= "name"/>
Password: <input type= "text" name= "password"/>
<input type= "Submit" value= "register"/>
</form>
</body>
(3) Fill in the following code in SECOND.JSP:
<body>
<%
Connection conn = null;
String driver= "Com.mysql.jdbc.Driver";//Database driver
String username= "root";//Database user name
String userpassword= "123";//Database Password
String dbname= "student";//Database name
String tablename= "PPP";//Table name
String url = "jdbc:mysql://localhost/" + DbName + "? user=" + UserName + "&password=" + userpassword;
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
conn = drivermanager.getconnection (URL);
String name=request.getparameter ("name");//Get form data
String password=request.getparameter ("password");
OUT.PRINTLN (name);//test the form data for outgoing
Statement stmt = Conn.createstatement ();
Stmt.executequery ("SET NAMES UTF8");
String sql = "INSERT into PPP values ('" "+name+" ', ' "+password+" ') ";
try{
Stmt.execute (SQL);
}
catch (Exception e) {}
Stmt.close ();
Conn.close ();
%>
</body>
(4) Program Flowchart:
Input data: 444 555
second.jsp Display:
The database displays:
(5) 444 555 has been added to the database. Only the basic learning, can learn better. How else would you know that better, a comparison
Jsp+mysql implementation increase, view function