当前位置:Gxlcms > 数据库问题 > jsp连接数据库 练习

jsp连接数据库 练习

时间:2021-07-01 10:21:17 帮助过:3人阅读

"utf-8"); String id = request.getParameter("id"); String sex = request.getParameter("sex"); String age = request.getParameter("age"); Connection con = null; Statement st = null; ResultSet rs = null; //注册驱动 try { Class.forName("com.mysql.jdbc.Driver"); //加载驱动 String url = "jdbc:mysql://localhost:3306/jdbc"; String user = "root"; String password = "root"; con = DriverManager.getConnection(url, user, password); //创建语句 st = con.createStatement(); String sql = "insert into ceshi(id,sex,age)" + "value(" + id + ",‘" + sex + "‘,‘" + age + "‘)"; int row = st.executeUpdate(sql); // 5.处理结果 if (row > 0) { response.sendRedirect("text.jsp"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } finally { try { if (st != null) { st.close(); } } finally { if (con != null) { con.close(); } } } } %> <br> </body>
<body>
    <%
        /*
        String id=request.getParameter("id");
        byte b[]=id.getBytes("UTF-8");
        id=new String(b,"UTF-8");
            String sex=request.getParameter("sex");
        byte b1[]=sex.getBytes("UTF-8");
        sex=new String(b1,"UTF-8");
            String age=request.getParameter("age");
        byte b2[]=age.getBytes("UTF-8");
        age=new String(b2,"UTF-8");
        */
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        //注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //加载驱动
            String url = "jdbc:mysql://localhost:3306/jdbc";
            String user = "root";
            String password = "root";
            con = DriverManager.getConnection(url, user, password);
            //创建语句
            st = con.createStatement();
            // rs = st.executeQuery("select id,sex,age from ceshi");
            // 5.处理结果
            /*while (rs.next()) {
                out.println(rs.getInt("id") + "\t"
                        + rs.getString("sex") + "\t" + rs.getInt("age"));}*/
    %>
    <table align="center">
        <tr>
            <td>id:</td>
            <td>性别:</td>
            <td>年龄:</td>
        </tr>
        <%
            rs = st.executeQuery("select id,sex,age from ceshi");
        %>
        <%
            while (rs.next()) {
        %>
        <tr>
            <td><%=rs.getInt("id")%></td>
            <td><%=rs.getString("sex")%></td>
            <td><%=rs.getInt("age")%></td>
        </tr>
        <%
            }
        %>
        <%
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                } finally {
                    try {
                        if (st != null) {
                            st.close();
                        }
                    } finally {
                        if (con != null) {
                            con.close();
                        }
                    }
                }

            }
        %>
    </table>
</body>

技术图片

 

 技术图片

 

jsp连接数据库 练习

标签:out   png   byte   host   connect   cte   ali   mys   mysq   

人气教程排行