- <%@ page contentType="text/html; charset=gb2312" %>
- <%@ page language="java" %>
- <%@ page import="com.mysql.jdbc.Driver" %>
- <%@ page import="java.sql.*" %>
- <%
- //加载驱动程序
- String driverName="com.mysql.jdbc.Driver";
- //数据库信息
- String userName="root";
- //密码
- String userPasswd="123";
- //数据库名
- String dbName="Student";
- //表名
- String tableName="stu_info";
- //将数据库信息字符串连接成为一个完整的url(也可以直接写成url,分开写是明了可维护性强)
- String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection conn=DriverManager.getConnection(url);
- Statement stmt = conn.createStatement();
- String sql="SELECT * FROM "+tableName;
- ResultSet rs = stmt.executeQuery(sql);
- out.print("id");
- out.print("|");
- out.print("name");
- out.print("|");
- out.print("phone");
- out.print("<br>");
- while(rs.next()) {
- out.print(rs.getString(1)+" ");
- out.print("|");
- out.print(rs.getString(2)+" ");
- out.print("|");
- out.print(rs.getString(3));
- out.print("<br>");
- }
- out.print("<br>");
- out.print("ok, Database Query Successd!");
- rs.close();
- stmt.close();
- conn.close();
- %>
在浏览器端打开我们的页面 类似于:http://localhost:8080/Test/test.jsp就可以看到查询的数据了。
from: http://blog.csdn.net/kirinlau/article/details/53182789
一个简单的JSP 连接MySQL使用实例
标签:配置 out statement path 需要 cut msi ble rac