zoukankan      html  css  js  c++  java
  • 提交页面插入到数据库2.jsp

    <%@ page language="java"  import="java.util.*"  import="java.sql.*"  pageEncoding="utf-8" %>
    <html>
      <head>
      <title>利用PreparedStatement对象添加一条记录页面</title>
      </head>
      <body>
      <%
    String driver = "com.mysql.jdbc.Driver";
    // URL指向要访问的数据库名test1
    String url = "jdbc:mysql://127.0.0.1:3306/a";
    // MySQL配置时的用户名
    String user = "root";
    // Java连接MySQL配置时的密码
    String password = "root";
    try {
    // 1 加载驱动程序
    Class.forName(driver);
    // 2 连接数据库
    Connection conn = DriverManager.getConnection(url, user, password);
    // 3 用来执行SQL语句
    Statement statement = conn.createStatement();
    // 要执行的SQL语句


    String sql="Insert into stu_info(id,name,sex,age,weight,hight)values(?,?,?,?,?,?)";
    PreparedStatement pstmt=conn.prepareStatement(sql);
    request.setCharacterEncoding("utf-8");
    String id=request.getParameter("id");
    System.out.print(id);
    String name=request.getParameter("name");
    String sex=request.getParameter("sex");
    String age=request.getParameter("age");
    String weight=request.getParameter("weight");
    String hight=request.getParameter("hight");

    pstmt.setString(1,id);
    pstmt.setString(2,name);
    pstmt.setString(3,sex);
    pstmt.setString(4,age);
    pstmt.setString(5,weight);
    pstmt.setString(6,hight);
    int n=pstmt.executeUpdate();
    if(n==1){%>数据插入成功!<br><%}
    else{%>数据插入失败!<br> <%}
    if(pstmt!=null){pstmt.close();}
    if(conn!=null) { conn.close();}
    } catch (ClassNotFoundException e) {
    System.out.println("Sorry,can`t find the Driver!");
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    }
    %>
    </body>
    </html>

  • 相关阅读:
    Django 标签过滤器
    Python短路原则
    python学习之路 八 :面向对象编程基础
    python学习之路 七 :生成器、迭代器
    python学习之路 六 :装饰器
    python学习之路 五:函数式编程
    python学习之路 四 :文件处理
    python学习之路 三:字符编码
    机器学习流程管理
    pyspark 自定义聚合函数 UDAF
  • 原文地址:https://www.cnblogs.com/www-hsy-com/p/7794800.html
Copyright © 2011-2022 走看看