栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

网站建设之在jdbc中连接数据库 实现查询和添加用户信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

网站建设之在jdbc中连接数据库 实现查询和添加用户信息

1.查询

效果展示:

前端页:

后台页:

代码:

前端:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




查询


    请选择查询的条件
性别:男

年龄:

             

 后台:

<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




结果


    <%
//        先连接数据库
        String driverName = "com.mysql.jdbc.Driver";         //驱动程序名
   String userName = "root";                            //数据库用户名
   String userPwd = "123456";                           //密码
   String dbName = "website";                           //数据库名
   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);  //创建连接对象
        request.setCharacterEncoding("UTF-8");//设置字符编码,避免出现乱码
        String sex = request.getParameter("sex");
        int age=Integer.parseInt(request.getParameter("age"));
        String sql = "select * from student where sex=? and age=?";
        PreparedStatement preparedStatement = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);//解决游标不能移动问题
        preparedStatement.setString(1,sex);
        preparedStatement.setInt(2,age);
        ResultSet resultSet = preparedStatement.executeQuery();//执行sql语句用来返回单个 ResultSet 对象
        resultSet.last();//移动到最后一条记录
    %>
        
你要查询的学生数据表中共有 <%=resultSet.getRow()%>人 <% resultSet.beforeFirst(); while (resultSet.next()){ %> <%}%>
记录条数 学号 姓名 性别 年龄
<%=resultSet.getRow()%> <%=resultSet.getString("no")%> <%=resultSet.getString("name")%> <%=resultSet.getString("sex")%> <%=resultSet.getInt("age")%>
<% if (resultSet!=null){ resultSet.close(); } if (preparedStatement!=null){ preparedStatement.close(); } if (conn!=null){ conn.close(); } %>

2.添加

效果展示:

前端页:

代码:

前端:

<%@page contentType="text/html" pageEncoding="GB2312"%>

      添加  
    
       
学号
姓名
性别
年龄
    

后端:

<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>

      
    添加结果 
  
   
    <%
      String driverName = "com.mysql.jdbc.Driver";         //驱动程序名
      String userName = "root";                            //数据库用户名
      String userPwd = "123456";                           //密码
      String dbName = "website";                          //数据库名
      String  url1="jdbc:mysql://localhost:3306/"+dbName;
      String url2 ="?user="+userName+"&password="+userPwd;
      String  url3="&useUnicode=true&characterEncoding=GB2312";
      String url =url1+url2+url3;                   //形成带数据库读写编码的数据库连接字
      Class.forName(driverName);
      Connection conn=DriverManager.getConnection(url); 
      String sql="insert into student(no,name, sex, age) values(?,?,?,?)";
      PreparedStatement  pstmt= conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //括号内解决游标不能移动  
      request.setCharacterEncoding("GB2312");//设置字符编码,避免出现乱码
      String no=request.getParameter("no");     
      String name=request.getParameter("name");
      String sex=request.getParameter("sex");
      int age=Integer.parseInt(request.getParameter("age"));
      
      pstmt.setString(1,no);
      pstmt.setString(2,name);
      pstmt.setString(3,sex);     
      pstmt.setInt(4,age);
      try{ 
          int n=pstmt.executeUpdate();
          if(n==1){%>
              数据插入操作成功!
<%} else{%> 数据插入操作失败!
<%} }catch(Exception e){%> 更新过程出现异常错误!
<%=e.getMessage()%> <%; } if(pstmt!=null){ pstmt.close(); } if(conn!=null){ conn.close(); } %>

!需要注意的是,该代码能够实现的前提是配置好连接数据库的环境,需要下载对应的数据库驱动器,并把jar文件复制到lib文件下才可实现。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358544.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号