標籤:
準備工作:
(1)建立並使用資料庫:create database student; use test;
(2)建立表:create table ppp(sno char(7),sname char(8));
(3)插入一條資料:insert into ppp values(123,456);
(4)顯示資料:select*from ppp;(複習一下資料庫知識)
開始:
(1)開啟Myeclipse. 建立web project.(Mysql) 並在WebRoot建立一個Second.jsp
(2)在index.jsp中填寫入代碼:
<body>
<form action="Second.jsp",method="get">
使用者名稱:<input type="text" name="name"/>
密 碼:<input type="text" name="password"/>
<input type="submit" value="註冊"/>
</form>
</body>
(3)在Second.jsp中填寫如下代碼:
<body>
<%
Connection conn = null ;
String driver="com.mysql.jdbc.Driver";//資料庫驅動
String userName="root";//資料庫使用者名稱
String userPassword="123";//資料庫密碼
String dbName="student";//資料庫名
String tableName="ppp";//表名
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");//擷取表單資料
String password=request.getParameter("password");
out.println(name);//測試表單資料是否傳出來
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)程式流程圖:
輸入資料:444 555
Second.jsp顯示:
資料庫顯示:
(5)444 555已經加入到資料庫了。只有基礎學好了,才能學習更好的。不然你怎麼知道那個更好,的有比較
jsp+mysql實現增加,查看功能