<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String drivername="com.mysql.jdbc.Driver";
String user="root";
String usepwd="test";
String dbname="sys";
String url1="jdbc:mysql://localhost:3306/"+dbname;
String url2="?user="+user+"&password="+usepwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url);
String sql="select * from java";
/*Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);*/两种方法均可
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
out.print("学号"+"            "+"姓名"+"<br>");
while(rs.next()){
out.print(rs.getInt("id")+"    ");
out.print(rs.getString("name")+"    ");
out.print("<br>");
}
rs.close();
stmt.close();
conn.close();
%>
</body>
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String drivername="com.mysql.jdbc.Driver";
String user="root";
String usepwd="test";
String dbname="sys";
String url1="jdbc:mysql://localhost:3306/"+dbname;
String url2="?user="+user+"&password="+usepwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url);
String sql="select * from java";
/*Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);*/两种方法均可
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
out.print("学号"+"            "+"姓名"+"<br>");
while(rs.next()){
out.print(rs.getInt("id")+"    ");
out.print(rs.getString("name")+"    ");
out.print("<br>");
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
ps:使用Navicat时一定要连网,不然Navicat打不开。