jsp连接mysql数据库 报错(急!!!)
我按照网上教的连接方法,结果报错,上面写:org.apache.jasper.JasperException:AnexceptionoccurredprocessingJ...
我按照网上教的连接方法,结果报错,上面写:
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 27
24: <%
25: Class.forName("org.gjt.mm.mysql.Driver").newInstance();
26: String url ="jdbc:mysql://localhost:3306/xszy?user=admin &password='11111'";
27: java.sql.Connection connection = java.sql.DriverManager.getConnection(url);
28: java.sql.Statement statement =connection.createStatement();
29: String sql="select * from dbtest";
30: java.sql.ResultSet rs = statement.executeQuery(sql);
请高手帮助啊!!!! 展开
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 27
24: <%
25: Class.forName("org.gjt.mm.mysql.Driver").newInstance();
26: String url ="jdbc:mysql://localhost:3306/xszy?user=admin &password='11111'";
27: java.sql.Connection connection = java.sql.DriverManager.getConnection(url);
28: java.sql.Statement statement =connection.createStatement();
29: String sql="select * from dbtest";
30: java.sql.ResultSet rs = statement.executeQuery(sql);
请高手帮助啊!!!! 展开
3个回答
展开全部
首先你确定你把MySQL的jdbc驱动放在了WEB_INF/lib目录下了吗?如果是的,你可以先把org.gjt.mm.mysql.Driver改成com.mysql.jdbc.Driver试试,前者是较早版本。
另外建议你写一个获得MySQL连接的单一类:
如下:
import java.sql.*;
import javax.sql.*;
public class JDBCConnector {
Connection conn;
String url;
String user;
String pws;
public JDBCConnector() {
url="jdbc:mysql://localhost/rQQ?useUnicode=true&characterEncoding=utf-8";
user="root";
pws="104814";
conn=null;
}
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url,user,pws);
}
catch (Exception e) {
System.out.println("Error occurred obtaining connection");
System.out.println(e.getMessage());
}
return conn;
}
}
另外建议你写一个获得MySQL连接的单一类:
如下:
import java.sql.*;
import javax.sql.*;
public class JDBCConnector {
Connection conn;
String url;
String user;
String pws;
public JDBCConnector() {
url="jdbc:mysql://localhost/rQQ?useUnicode=true&characterEncoding=utf-8";
user="root";
pws="104814";
conn=null;
}
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection(url,user,pws);
}
catch (Exception e) {
System.out.println("Error occurred obtaining connection");
System.out.println(e.getMessage());
}
return conn;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1、你的password 不用带单引号;
2、貌似用户名后面多了一个空格;
2、貌似用户名后面多了一个空格;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","111111");
你的url写法会不会不行?
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","111111");
你的url写法会不会不行?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询