jsp连接mysql数据库

jar包导入;

 

代码:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312" %>
    <%@ page import ="java.sql.Connection" %>
    <%@ page import ="java.sql.DriverManager" %>
    <%@ page import ="java.sql.ResultSet" %>
    <%@ page import ="java.sql.SQLException" %>
    <%@ page import ="java.sql.DriverManager" %>
    <%@ page import="java.sql.Statement" %>
<html>
<body>
<%
Connection con;
Statement sql;
ResultSet rs;
try{Class.forName("com.mysql.jdbc.Driver").newInstance();}
catch(Exception e){out.print(e);}
try{
    String uri="jdbc:mysql://localhost:3306/mysql";
    con=DriverManager.getConnection(uri,"root","×××××××");
    sql=con.createStatement();
    rs=sql.executeQuery("SELECT Host,User FROM user");
    out.print("<table border=2>");
    out.print("<tr>");
    out.print("<th width=100>"+"Host");
    out.print("<th width=100>"+"User");
    out.print("</tr>");
    while(rs.next()){
        out.print("<tr>");
        out.print("<td>"+rs.getString(1)+"</td>");
        out.print("<td>"+rs.getString(2)+"</td>");
        out.print("</tr>");
    }
    out.print("</table>");
    con.close();
}
catch(SQLException e1){out.print(e1);}
%>
</body>
</html>

 

posted @ 2016-04-03 21:29  Arikes  阅读(293)  评论(0编辑  收藏  举报