Code these days look to eclipse jsp mysql problems encountered in the operation
1. Run the code found error can not find Class.forName ( "com.mysql.jdbc.Driver") newInstance ( );.
Note that: the Java project just import jdbc jar package in the library inside the
web project to re import jar package lib in the web-inf
2. create a database connection error conn
solution to the URL into the format shown in the code of the
reason I did not get to know now (if anyone knows the reason please tell me thank you) anyway, the very pit to find a lot of information to find solutions
, Jdbc: mysql: //127.0.0.1: 3306 / ****** characterEncoding = utf8 & useSSL = false & serverTimezone = UTC & allowPublicKeyRetrieval = true?
Resolved:
characterEncoding = utf8 (character encoding)
useSSL = false (since version 8 was found to be necessary to add , 5.X impression is not required, this parameter may add SSL connection setup and MySQL relationship)
serverTimezone = UTC (when the database connection time, time Zone error adding this parameter, I seemingly appear when using connection pooling Druid this problem)
allowPublicKeyRetrieval = to true (using the root account login is no problem using ordinary account will prompt Public Key Retrieval error)
reference link: reference Links
When you're ready to success:
<%@page contenxType="text/html;character="utf-8" language="java"
import="com.mysql.jdbc.Driver"
import="java.sql.*"%>
//加载驱动程序 数据库信息 密码 数据库名
String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPasswd="-------";//你的数据库密码
String dbName="ch4_3_user";
String tableName="user";
//注意这里的URL
String URL="jdbc:mysql://localhost/ch4_3_user?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&
rewriteBatchedStatements=true";//
//注意这里的URL写法
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn=DriverManager.getConnection(URL,userName,userPasswd);
Statement stmt = conn.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs = stmt.executeQuery(sql);
out.priint("id");
out.print("--");
out.priint("name");
out.print("--");
out.priint("pwd");
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>");
}
rs.close();
stmt.close();
conn.close();
%>