jsp+mysql實現增加,查看功能

來源:互聯網
上載者:User

標籤:

準備工作:

(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實現增加,查看功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.