用jsp连接数据库老报错,本人刚学习Java web的环境搭建,代码和错误提示如下: 在这里先谢谢各位了!!!

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 8 in the jsp file: /insertUser.jsp
Connection cannot be resolved to a type
5: <%
6:
7: Class.forName("com.mysql.jdbc.Driver").newInstance();
8: Connection con=Java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ch2","root","abcd1234!");
9: con.close();
10: out.println("OK");
11:

An error occurred at line: 8 in the jsp file: /insertUser.jsp
Java.sql.DriverManager cannot be resolved to a type
5: <%
6:
7: Class.forName("com.mysql.jdbc.Driver").newInstance();
8: Connection con=Java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ch2","root","abcd1234!");
9: con.close();
10: out.println("OK");
11:

代码是:
<%@ page contentType="text/html; charset=UTF-8" language="Java" import="Java.sql.*,Java.io.*"%>
<html>
<body>
<center>往数据库中添加数据:<hr>
<%

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=Java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ch2","root","abcd1234!");
con.close();
out.println("OK");
%>
<center>
</body>
</html>
就是连不上,伤脑筋啊!

第1个回答  2013-03-03
你代码写的有问题,java连接数据库貌似有几个步骤的
Ok, 你试着:
<%
Connection conn = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.34.59:3306/edms?useServerPrepStmts=true&userUnicode=true&characterEncoding=utf-8";
String username = "root";
String password = "1234";
try {
//加载驱动是第一步
Class.forName(driver).newInstance();
//建立连接是第二步
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//创建statement
Statement stmt = conn.getConn().createStatement();
//得到结果集
ResultSet rs = stmt.executeQuery("select * from table_check order by id");
//结果集的遍历你应该会的

//关闭你用过的东西
stmt.close();
conn.close();

%>
建议连接数据库的东西在后台写,不要嵌入在html带代码中追问

刚才按您的写法试了,怎么还是报这个错误。。呜,伤不起啊
exceptionorg.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 6 in the jsp file: /insertUser.jsp
Connection cannot be resolved to a type
3:
4: 往数据库中添加数据:
5: <%
6: Connection conn=null;

第2个回答  2013-03-07
在头上加入下面的包,Connection cannot be resolved to a type 错误上都报了,这个连接不是一种类型。你上面没有导包没办法引用。
<%@page import="java.sql.*" %>
%@page import="java.util.*" %
<%@page import="java.sql.Connection" %>
<%@page import="java.sql.DriverManager" %>
第3个回答  2013-03-03
Class.forName("com.mysql.jdbc.Driver").newInstance();

没见过这种用法直接Class.forName("com.mysql.jdbc.Driver")加载驱动就行了
而你刚打开连接什么都没做就关上有什么意义,我实在看不懂
第4个回答  2013-03-03
把Class.forName("com.mysql.jdbc.Driver").newInstance();中的newInstance()去掉看看
相似回答