zoukankan      html  css  js  c++  java
  • java web连接数据库添加记录模块

    数据库建立如下

    <%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>

    <html>
    <head>
    <title>利用PreparedStatement对象添加一条记录页面</title>
    </head>

    <body>
    <%String driverName="com.mysql.jdbc.Driver";
    String userName="root";
    String userPwd="123456";
    String dbName="student";
    String url1="jdbc:mysql://localhost:3306/"+dbName;
    String url2="?user="+userName+"&password="+userPwd;
    String url3="&useUnicode=true&characterEncoding=UTF-8";
    String url=url1+url2+url3;
    Class.forName(driverName);
    Connection conn=DriverManager.getConnection(url);
    String sql="Insert into stu_info(id,name,sex,age,weight,hight)values(?,?,?,?,?,?)";
    PreparedStatement pstmt=conn.prepareStatement(sql);
    pstmt.setInt(1,16);
    pstmt.setString(2,"zhangsan");
    pstmt.setString(3,"man");
    pstmt.setInt(4,20);
    pstmt.setFloat(5,70);
    pstmt.setFloat(6,175);
    int n=pstmt.executeUpdate();
    if(n==1){%>数据插入成功!<br><%}
    else{ %>数据插入失败!b<br><%}
    if(pstmt!=null){pstmt.close(); }
    if(conn!=null){conn.close();}
    %>
    </body>
    </html>

  • 相关阅读:
    day89
    day88
    day87
    day86
    day85
    day84
    day83
    Maven仓库汇总
    [转载]AngularJS入门教程04:双向绑定
    [转载]AngularJS入门教程03:迭代器
  • 原文地址:https://www.cnblogs.com/ljs-666/p/7689176.html
Copyright © 2011-2022 走看看