码迷,mamicode.com
首页 > 数据库 > 详细

jsp+mysql实现增加,查看功能

时间:2016-05-07 11:22:42      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

准备工作:

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

标签:

原文地址:http://blog.csdn.net/sky_sea_desert_me/article/details/51333787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!