一个简单的JSP 连接MySQL使用实例
时间:2021-07-01 10:21:17
帮助过:26人阅读
- <%@ 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";
-
- 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