jabc连接数据库,处理添加数据时的乱码问题

来源:互联网 发布:梁朝伟 董洁 知乎 编辑:程序博客网 时间:2024/05/17 06:28


<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>通过MySQL的JDBC驱动访问数据库</title>
    </head>
    <body>
        <h2>使用MySQL的JDBC驱动访问MySQL数据库</h2>
        <hr>
        <table border="1" bgcolor="#ccceee"  align="center">
            <tr>
                <th width="87" align="center">学号</th>
                <th width="87" align="center">姓名</th>
                <th width="87" align="center">专业</th>
            </tr>
            <%
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.jdbc.Driver");
                /*3306为端口号,student为数据库名,url后面加的?useUnicode=true&characterEncoding=gbk,是为了处理向数据库中添加数据时出现乱码的问题。*/
                String url="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=gbk";
                con=DriverManager.getConnection(url,"root","root");
                stmt=con.createStatement();
                String sql="select * from sinfo";
                rs=stmt.executeQuery(sql);
                while(rs.next()){
            %>                         
           <tr>
               <td><%=rs.getString("studentNumber")%></td>
               <td><%=rs.getString("studentName")%></td>
               <td><%=rs.getString("studentClass")%></td>
                      
           </tr>
           <%
                }
                rs.close();
                stmt.close();
                con.close();
            %>
       </table>
       <hr>
    </body>
</html>
0 0
原创粉丝点击