<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*"%> <html> <body> <%Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection con; Statement stmt;//(或PreparedStatement) ResultSet rs; //加载驱动程序 Class.forName("com.mysql.jdbc.Driver");//这个是新的 //Class.forName("org.gjt.mm.mysql.Driver");这个是旧的 //注册mysql驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); String url="jdbc:localhost:3306(端口)/first"; String user="root";//(username) String password="00"; con=DriverManager.getConnection(url,user,password); stmt=con.createStatement(); rs=stmt.executeQuery(select * from about); while(rs.next()) {%> 您的第一个字段内容为:<%=rs.getString(1)%> 您的第二个字段内容为:<%=rs.getString(2)%> <%}%> <%out.print("数据库操作成功,恭喜你");%> <%rs.close(); stmt.close(); con.close(); %> </body> </html>这样的页面不能连接数据库 不知为何 错误如下: 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 JSPAn error occurred at line: 5 in the jsp file: /first.jspGenerated servlet error: [javac] Compiling 1 source fileC:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:56: ')' expected rs=stmt.executeQuery(select * from about); ^ 1 error org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351) org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415) org.apache.jasper.compiler.Compiler.compile(Compiler.java:458) org.apache.jasper.compiler.Compiler.compile(Compiler.java:439) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) note The full stack trace of the root cause is available in the Tomcat logs.
难道是(select * from about)改为(("select * from about"))? 又出现错误: org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 5 in the jsp file: /first.jspGenerated servlet error: [javac] Compiling 1 source fileC:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:50: package com.mysql.jdbc does not exist DriverManager.registerDriver(new com.mysql.jdbc.Driver()); ^ 1 error org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127) org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351) org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415) org.apache.jasper.compiler.Compiler.compile(Compiler.java:458) org.apache.jasper.compiler.Compiler.compile(Compiler.java:439) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248) javax.servlet.http.HttpServlet.service(HttpServlet.java:856) note The full stack trace of the root cause is available in the Tomcat logs.
try{
Connection con;
Statement stmt;(或PreparedStatement)
ResultSet rs;
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//这个是新的
//Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
//注册mysql驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:localhost:3306(端口)/dbName";
String user="root";(username)
String password=userpassword;
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
rs=stmt.executeQuery(select * from table);
.................
用google搜一下,网上有很多这方面的资料.
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con;
Statement stmt;//(或PreparedStatement)
ResultSet rs;
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");//这个是新的
//Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
//注册mysql驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:localhost:3306(端口)/first";
String user="root";//(username)
String password="00";
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
rs=stmt.executeQuery(select * from about);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
con.close();
%>
</body>
</html>这样的页面不能连接数据库
不知为何
错误如下:
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 JSPAn error occurred at line: 5 in the jsp file: /first.jspGenerated servlet error:
[javac] Compiling 1 source fileC:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:56: ')' expected
rs=stmt.executeQuery(select * from about);
^
1 error
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
note The full stack trace of the root cause is available in the Tomcat logs.
又出现错误:
org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 5 in the jsp file: /first.jspGenerated servlet error:
[javac] Compiling 1 source fileC:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\jsp-examples\org\apache\jsp\first_jsp.java:50: package com.mysql.jdbc does not exist
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
^
1 error
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:127)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:415)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:458)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:552)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
note The full stack trace of the root cause is available in the Tomcat logs.
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con;
Statement stmt;//(或PreparedStatement)
ResultSet rs;
//加载驱动程序
//Class.forName("com.mysql.jdbc.Driver");//这个是新的
//Class.forName("org.gjt.mm.mysql.Driver");这个是旧的
//注册mysql驱动程序
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:localhost:3306/first";
String user="root";//(username)
String password="00";
con=DriverManager.getConnection(url,user,password);
stmt=con.createStatement();
rs=stmt.executeQuery("select * from about");
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
con.close();
%>
</body>
</html>因为你一开始就用了旧的驱动,但是后面又用新的,这样造成了混淆.
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
这一句我一直都没有写的,好像是已经注册了的,所以可以省略.
你写的时候最好用上try...catch语句,追踪一下可能出错的地方,比如说连不数据库又怎样处理.