eclipse+tomcat5.0 开发jsp连接MYSQL的问题,急!搞了2天了!

itfly 2006-02-20 12:07:36
myjsp.jsp

<%@ page language="java" import="java.util.*,java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<%
String s="h";
String url="jdbc:mysql://localhost:3306/myweb?user=root&password=adminuser";
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e)
{
out.print("err");
}
Connection conn = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/myweb","root","adminuser");
}catch(SQLException e)
{
out.print(e);
}
if(conn==null)
out.print("error");
out.print("good");
%>
</body>
</html>

...全文
420 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
itfly 2006-02-21
  • 打赏
  • 举报
回复
搞定
stckiss 2006-02-21
  • 打赏
  • 举报
回复
看了半天,你没写端口号"3306"
itfly 2006-02-20
  • 打赏
  • 举报
回复
JAVA_HOME
已经设置好了的啊:
C:\j2sdk1.4.2_04

ecpise使用是3。0
tomcat5.0
itfly 2006-02-20
  • 打赏
  • 举报
回复
控制台的信息为:
jdbc:mysql://localhost/myweb
说明:
conn = DriverManager.getConnection("jdbc:mysql://localhost/myweb","root","adminuser");
出错,但错在那里啊?
doway 2006-02-20
  • 打赏
  • 举报
回复
设置 JAVA_HOME 环境变量到你的 JDK 安装目录,再用 conn = DriverManager.getConnection(url); 试试,你的代码中已经定义了 url 却未使用。
itfly 2006-02-20
  • 打赏
  • 举报
回复

mysql类

public class mysql {

public static Connection getConnection()
{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e)
{
//com.mysql.jdbc.driver"
System.out.print("driver");
}
Connection conn = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost/myweb","root","adminuser");

}catch(SQLException e)
{
System.out.print("jdbc:mysql://localhost/myweb");
}
return conn;
}
}
itfly 2006-02-20
  • 打赏
  • 举报
回复

servlet+javabean实现方法:

servlet 部门:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 创建日期 2006-2-20
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

/**
* @author IBM
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Hi extends HttpServlet {

/**
* Constructor of the object.
*/
public Hi() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(mysql.getConnection()==null)
{
out.print("error");
}else
{
out.print("good");
}
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}
itfly 2006-02-20
  • 打赏
  • 举报
回复
但我把那条语句去掉以后就不会出错了
doway 2006-02-20
  • 打赏
  • 举报
回复
倒,楼主,提示说是找不到编译器类,因此你的应用服务器应该不能够正确处理任何一个 JSP 文件才对,与具体的代码没有关系吧。
itfly 2006-02-20
  • 打赏
  • 举报
回复
conn = DriverManager.getConnection("jdbc:mysql://localhost/myweb","root","adminuser");
主要是这句的问题,但不知道问题在那!
doway 2006-02-20
  • 打赏
  • 举报
回复
tools.jar 找不到。
itfly 2006-02-20
  • 打赏
  • 举报
回复
提示错误:
2006-2-21 0:04:53 org.apache.jasper.compiler.Compiler generateClass
严重: Javac exception
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
at org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(CompilerAdapterFactory.java:106)
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:935)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:764)
at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:382)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:472)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:451)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:511)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:295)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at sun.reflect.GeneratedMethodAccessor61.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:239)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Unknown Source)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:268)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:157)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:50)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:140)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:136)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Unknown Source)
2006-2-21 0:04:53 org.apache.jasper.compiler.Compiler generateClass
严重: Env: Compile: javaFileName=/D:/tomcat5/work/Catalina/localhost/eweb//org/apache/jsp\MyJsp_jsp.java
classpath=/D:/tomcat5/webapps/eweb/WEB-INF/classes/;/D:/tomcat5/webapps/eweb/WEB-INF/lib/mysql-connector-java-3.1.12-bin.jar;D:\tomcat5\work\Catalina\localhost\eweb;/D:/tomcat5/webapps/eweb/WEB-INF/classes/;/D:/tomcat5/webapps/eweb/WEB-INF/lib/mysql-connector-java-3.1.12-bin.jar;D:/tomcat5/shared/classes/;D:/tomcat5/common/classes/;D:/tomcat5/common/endorsed/xercesImpl.jar;D:/tomcat5/common/endorsed/xml-apis.jar;D:/tomcat5/common/lib/ant-launcher.jar;D:/tomcat5/common/lib/ant.jar;D:/tomcat5/common/lib/commons-collections-3.1.jar;D:/tomcat5/common/lib/commons-dbcp-1.2.1.jar;D:/tomcat5/common/lib/commons-el.jar;D:/tomcat5/common/lib/commons-pool-1.2.jar;D:/tomcat5/common/lib/jasper-compiler.jar;D:/tomcat5/common/lib/jasper-runtime.jar;D:/tomcat5/common/lib/jsp-api.jar;D:/tomcat5/common/lib/naming-common.jar;D:/tomcat5/common/lib/naming-factory.jar;D:/tomcat5/common/lib/naming-java.jar;D:/tomcat5/common/lib/naming-resources.jar;D:/tomcat5/common/lib/servlet-api.jar;/D:/tomcat5/bin/bootstrap.jar;/C:/Program%20Files/Java/j2re1.4.2_04/lib/tools.jar;/C:/Program%20Files/Java/j2re1.4.2_04/lib/ext/dnsns.jar;/C:/Program%20Files/Java/j2re1.4.2_04/lib/ext/ldapsec.jar;/C:/Program%20Files/Java/j2re1.4.2_04/lib/ext/localedata.jar;/C:/Program%20Files/Java/j2re1.4.2_04/lib/ext/sunjce_provider.jar
cp=D:\tomcat5\bin\bootstrap.jar;C:\Program Files\Java\j2re1.4.2_04\lib\tools.jar
cp=D:\tomcat5\webapps\eweb\WEB-INF\classes
cp=D:\tomcat5\webapps\eweb\WEB-INF\lib\mysql-connector-java-3.1.12-bin.jar
cp=D:\tomcat5\work\Catalina\localhost\eweb
cp=D:\tomcat5\webapps\eweb\WEB-INF\classes
cp=D:\tomcat5\webapps\eweb\WEB-INF\lib\mysql-connector-java-3.1.12-bin.jar
cp=D:\tomcat5\shared\classes
cp=D:\tomcat5\common\classes
cp=D:\tomcat5\common\endorsed\xercesImpl.jar
cp=D:\tomcat5\common\endorsed\xml-apis.jar
cp=D:\tomcat5\common\lib\ant-launcher.jar
cp=D:\tomcat5\common\lib\ant.jar
cp=D:\tomcat5\common\lib\commons-collections-3.1.jar
cp=D:\tomcat5\common\lib\commons-dbcp-1.2.1.jar
cp=D:\tomcat5\common\lib\commons-el.jar
cp=D:\tomcat5\common\lib\commons-pool-1.2.jar
cp=D:\tomcat5\common\lib\jasper-compiler.jar
cp=D:\tomcat5\common\lib\jasper-runtime.jar
cp=D:\tomcat5\common\lib\jsp-api.jar
cp=D:\tomcat5\common\lib\naming-common.jar
cp=D:\tomcat5\common\lib\naming-factory.jar
cp=D:\tomcat5\common\lib\naming-java.jar
cp=D:\tomcat5\common\lib\naming-resources.jar
cp=D:\tomcat5\common\lib\servlet-api.jar
cp=D:\tomcat5\bin\bootstrap.jar
cp=C:\Program%20Files\Java\j2re1.4.2_04\lib\tools.jar
cp=C:\Program%20Files\Java\j2re1.4.2_04\lib\ext\dnsns.jar
cp=C:\Program%20Files\Java\j2re1.4.2_04\lib\ext\ldapsec.jar
cp=C:\Program%20Files\Java\j2re1.4.2_04\lib\ext\localedata.jar
cp=C:\Program%20Files\Java\j2re1.4.2_04\lib\ext\sunjce_provider.jar
work dir=D:\tomcat5\work\Catalina\localhost\eweb
extension dir=C:\Program Files\Java\j2re1.4.2_04\lib\ext
srcDir=D:\tomcat5\work\Catalina\localhost\eweb
include=org/apache/jsp/MyJsp_jsp.java

2006-2-21 0:04:53 org.apache.jasper.compiler.Compiler generateClass
严重: Error compiling file: /D:/tomcat5/work/Catalina/localhost/eweb//org/apache/jsp\MyJsp_jsp.java [javac] Compiling 1 source file


datalover 2006-02-20
  • 打赏
  • 举报
回复
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myweb","root","adminuser");
wsk_228 2006-02-20
  • 打赏
  • 举报
回复
CLASSPATH

C:\j2sdk1.4.2_04\lib\tools.jar;C:\j2sdk1.4.2_04\lib\dt.jar;.
itfly 2006-02-20
  • 打赏
  • 举报
回复
如果是JDK的错误的话,为什么一般的JSP不会出错呢?!加了数据库的就会出错?!
SL77756221 2006-02-20
  • 打赏
  • 举报
回复
conn = DriverManager.getConnection("jdbc:mysql://localhost/myweb","root","adminuser");(错误)

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myweb","root","adminuser");(正确)

你没有写端口号,就是那个“3306”
infowain 2006-02-20
  • 打赏
  • 举报
回复
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
是环境变量的问题
itfly 2006-02-20
  • 打赏
  • 举报
回复
C:\j2sdk1.4.2_04\lib\dt.jar
起什么做用,只有这个没有测试了。!

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧