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>

  • 相关阅读:
    网络通信
    jvm调优
    rokectMq
    mybatis属性类
    spring cloud feign
    spring cloud hystrix
    spring cloud ribbon
    mybatis(二)创建代理类执行sql
    内存操作函数
    堆空间的开辟与使用
  • 原文地址:https://www.cnblogs.com/www-hsy-com/p/7794800.html
Copyright © 2011-2022 走看看