Mysql + JSP Example

Java Database Connectivity (JDBC) is an API that lets you access any tabular data sources from a Java application.
This example shows a JSP that queries a UserInfo table using JDBC.







<%@ page language="java" import="java.sql.*, java.io.*" %>
<%
    Connection con = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/blog?user=root&password=123456");
        Statement statement = con.createStatement();
        ResultSet rs = statement.executeQuery("SELECT * FROM userinfo");

%>


<%
        while ( rs.next() ) {
            out.println("
\n


");
            out.println("


");
        }
        rs.close();
    } catch (IOException io) {
        out.println(io.getMessage());
    } catch (SQLException sql) {
        out.println(sql.getMessage());
    } catch (Exception e) {
        out.println(e.getMessage());
    } finally {
        try {
            if ( con != null ) {
                con.close();
            }
        } catch (SQLException sql) {
            out.println(sql.getMessage());
        }
    }
%>
Name password
" + rs.getString("username") + "" + rs.getString("password") + "

Tags:

One Response to “Mysql + JSP Example”

  1. teddy 28. Mar, 2010 at 2:04 pm #

    Great Example,thanks

Leave a Reply