<%@ 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="wenbin";
//数据库名
String dbName="first";
//表名
String tableName="about";
//联结字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
//获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rmeta.getColumnCount();
// 输出每一个数据值
out.print("id");
out.print("|");
out.print("num");
out.print("<br>");
while(rs.next()) {
out.print(rs.getString(1)+" ");
out.print("|");
out.print(rs.getString(2));
out.print("<br>");
}
out.print("<br>");
out.print("数据库操作成功,恭喜你");
rs.close();
statement.close();
connection.close();
%>
//以上代码有错吗?运行起来总报错:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.10
//各位,请帮忙看看!先谢谢了!
----------------解决方案--------------------------------------------------------
受不鸟LZ了
你好歹也写个类稍微封装下你的数据库连接啊!!也方便调试!!
你这么一大堆全写在jsp页面里,HoHo
看得我老人家眼睛都晕了!!哈哈
写个类封装下吧,这是个小问题,你自己检查下就可以解决的,动下脑筋嘛!!
----------------解决方案--------------------------------------------------------
我是刚学的。不太会!你们帮帮我嘛!
----------------解决方案--------------------------------------------------------
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
以上的这些语句都会抛出异常的,你用try,catch语句捕获 一下异常试试!
----------------解决方案--------------------------------------------------------
加上try,catch语句捕获:
错误如下:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: org.apache.jasper.JasperException: Unable to load class for JSP org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:154) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
org.apache.jasper.JasperException: Unable to load class for JSP org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:600) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:142) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.ClassNotFoundException: org.apache.jsp.test.first_jsp java.net.URLClassLoader$1.run(Unknown Source) java.security.AccessController.doPrivileged(Native Method) java.net.URLClassLoader.findClass(Unknown Source) org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:134) org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66) org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:598) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:142) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:320) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
----------------解决方案--------------------------------------------------------
是不是我的mysql数据库有问题?
----------------解决方案--------------------------------------------------------
String url="jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+"&password="+userPasswd;
这需要加上端口号的,同时要注意的是:一定要确认mysql驱动安装路径正确!
----------------解决方案--------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnection {
private static Connection conn = null;
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConnection()
{
String url = "jdbc:mysql://localhost:3306/first";
String user="root";
String password="wenbin";
try
{
conn = DriverManager.getConnection(url,user,password);
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
public static void close()
{
if(conn!=null)
{
try
{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
public void printResult(String tbName)
{
PreparedStatement stm = null;
String sql = "select * from "+tbName;
try
{
stm = getConnection().prepareStatement(sql);
ResultSet rss = stm.executeQuery();
while(rss.next())
{
System.out.print(rss.getString(1)+"|");
System.out.print(rss.getString(2)+"|");
System.out.print(rss.getString(3));
}
}catch(SQLException e){
e.printStackTrace();
}finally{
close();
}
}
public static void main(String[] args) {
new DBConnection().printResult("about");
//你将这段代码运行看看,是不是有数据输出.
//表名,数据库用户以及密码我都按照你的写的,你用改了直接运行看看
}
}
----------------解决方案--------------------------------------------------------
我用的端口是8080:
3306不用能。
http://localhost:8080/test.jsp
把程序都给输入了:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBConnection { private static Connection conn = null; static { try { Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection() { String url = "jdbc:mysql://localhost:8080/first"; String user="root"; String password="wenbin"; try { conn = DriverManager.getConnection(url,user,password); }catch(SQLException e){ e.printStackTrace(); } return conn; } public static void close() { if(conn!=null) { try { conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } public void printResult(String tbName) { PreparedStatement stm = null; String sql = "select * from "+tbName; try { stm = getConnection().prepareStatement(sql); ResultSet rss = stm.executeQuery(); while(rss.next()) { System.out.print(rss.getString(1)+"|"); System.out.print(rss.getString(2)+"|"); System.out.print(rss.getString(3)); } }catch(SQLException e){ e.printStackTrace(); }finally{ close(); } } public static void main(String[] args) { new DBConnection().printResult("about"); //你将这段代码运行看看,是不是有数据输出. //表名,数据库用户以及密码我都按照你的写的,你用改了直接运行看看 } }
----------------解决方案--------------------------------------------------------
对不起! 我傻掉了。忘<%%>了
----------------解决方案--------------------------------------------------------