1. 程式人生 > 實用技巧 >jsp連線資料庫

jsp連線資料庫

<body>
    <form action="tianjia.jsp">
        <table>
            <tr>
                <td>序號:</td>
                <td><input type="text" name="id"></td>
            </tr>
            <tr>
                <td>性別:</td>
                <td><input type="text" name="sex"></td>
            </tr>
            <tr>
                <td>年齡:</td>
                <td><input type="text" name="age"></td>
            </tr>
            <tr>
                <td><input type="submit" name="sub" value="提交"></td>
            </tr>
        </table>
    </form>
    <br>
</body>

main.jsp
<body>
    <%
        request.setCharacterEncoding("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> increase.jsp
<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>

slelect.jsp