当前位置: 代码迷 >> Java相关 >> ecplise中java 连数据库问题
  详细解决方案

ecplise中java 连数据库问题

热度:535   发布时间:2007-05-28 17:23:51.0
ecplise中java 连数据库问题

package mypackage;

import java.sql.*;

public class DataConnection
{
public static void main(String[] args)
{
String driver = "com.mysql.jdbc.driver";
String url = "jdbc:mysql://localhost/mydatabase";
String user="root";
String password="tiger";
try
{
Class.forName(driver);
Connection con= DriverManager.getConnection(url, user, password);
String insert ="insert friends(id,name) values (?,?)";
PreparedStatement prestatement=con.prepareStatement(insert);
String[] names={"liu","qian","shi","xu","zhou","lu"};
for(int i=0;i<6;i++)
{
prestatement.setInt(1, i);
prestatement.setString(2, names[i]);
prestatement.execute();
}
String query="select * from friends";
ResultSet result=(ResultSet) prestatement.executeQuery(query);
while(result.next())
{
System.out.println(result.getString("name"));
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}

}

}
这是我的程序
已经加载了mysql-connector-java-5.0.6-bin.jar和mysql-connector-java-5.0.6-bin-g.jar包
但总是出现
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at mypackage.DataConnection.main(DataConnection.java:16)
这个错误
明明驱动我已经加载了
mysql我用的是5.0
我在网上搜了几天,都没有解决这个问题
大家帮帮我吧


[讨论]ecplise中jsp+mysql问题和大家分享讨论

我用的是ecplise3.2+myecplise5.1.1+Tomcat 5.0+MySQL5.0+jdk1.5
新建项目名为Test,然后在里面新建了mysql和mysql.jsp
功能是把mysql写的东西传到mysql.jsp中
程序如下:
(2.1)mysql

<html>
<head>
<title>mysql</title>
</head>
<body>
<form name="form" action="mysql.jsp" method="post">
<p>姓:<input name ="last_name" type="text" id="last_name"></p>
<p>名:<input name="first_name" type="text" id="first_name"></p>
<p>
<input type="submit" value="提交">
<input type="reset" value="取消">
</p>
</form>
</body>
</html>

(2.2)mysql.jsp

<%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'mysql.jsp' starting page</title>
</head>
<body>
<h2>将信息存人mysql 中</h2>
<%
Connection con=null;
Statement stmt=null;
Statement stmt1=null;
ResultSet rs=null;
request.setCharacterEncoding("gb2312");
String employee_id=null;
String last_name=request.getParameter("last_name");
String first_name=request.getParameter("first_name");
String birth="1986/10/10";
String sex="F";
String new_last_name="";
String new_first_name="";
%>
从mysql接收的信息如下:<br>
姓:<%=last_name %>  
名:<%=first_name %>
<%
try
{
Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://localhost/sample_db";
String user="root";
String pass="tiger";
con=DriverManager.getConnection(url,user,pass);
stmt=con.createStatement();
String upd="insert into employee(employee_id,last_name,first_name,birth,sex) values ("+employee_id+",'"+last_name+"','"+first_name+"','"+birth+"','"+sex+"')";
stmt.executeUpdate(upd);
stmt1=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query="select * from employee";
rs=stmt1.executeQuery(query);
rs.last();
new_last_name=rs.getString("last_name");
new_first_name=rs.getString("first_name");
stmt.close();
stmt1.close();
con.close();
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException sqle)
{
out.println("sqle="+sqle);
}
}
%>
从employee中取出的新增姓名:<br>
新增姓名:<%=new_last_name+new_first_name %><br>
</body>
</html>

创建一个数据库sample_db并在它下面新建了一个表employee,语句如下
create table employee(
employee_id int unsigned not null auto_increment primary key,
last_name varchar(15) not null,
first_name varchar(15) not null,
birth date not null,
sex enum('M','F') default 'M'
);

当我通过右击项目名,然后选择properties->java build path,选择Libraries标签,
单击Add Externar JARs把mysql-connector-java-5.0.6-bin.jar和mysql-connector-java-5.0.6-bin-g.jar

这2个包加进去时,在mysql提交时,会出现如下异常
javax.servlet.ServletException: org.gjt.mm.mysql.Driver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.mysql_jsp._jspService(mysql_jsp.java:141)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:148)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:69)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.mysql_jsp._jspService(mysql_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


意思是说没有找到jdbc驱动:ClassNotFoundException
于是我就把刚才加的2个包给移除了
这时我把刚才2个包拷贝到该项目的lib目录下:Test\WebRoot\WEB-INF\lib
重启tomcat,在mysql提交时,又出现如下异常
javax.servlet.ServletException: org/aspectj/lang/Signature
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.mysql_jsp._jspService(mysql_jsp.java:141)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.mysql_jsp._jspService(mysql_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

我上网搜了一下是什么原因,说是少了这个jar包aspectj-1.5.3.jar
于是下了一个,网址是从http://www.eclipse.org/aspectj/downloads.php
解开aspectj-1.5.3.jar 将其中下面的lib包里面的aspectjrt.jar包放到该项目的lib目录下:Test\WebRoot\WEB-INF\lib
即现在该项目lib目录下有3个jar包:mysql-connector-java-5.0.6-bin.jar,
mysql-connector-java-5.0.6-bin-g.jar,aspectj-1.5.3.jar
重启tomcat然后运行项目,一切正常

希望能给大家带来帮助,同时谁知道以上的原因,就一起讨论下哦

[此贴子已经被作者于2007-5-31 17:05:38编辑过]

搜索更多相关的解决方案: java  数据库  String  ecplise  

----------------解决方案--------------------------------------------------------

不好意思,今天心情不好没有仔细看

[此贴子已经被作者于2007-5-28 18:08:44编辑过]


----------------解决方案--------------------------------------------------------
以下是引用红影在2007-5-28 17:42:29的发言:
Connection con= DriverManager.getConnection(url, user, password);
数据库的用户名user和密码password是什么就写什么,没有的话就留空

根楼主所问无关,鉴定完毕~~~
----------------解决方案--------------------------------------------------------

你们知道问题在哪儿吗?
教教我啊


----------------解决方案--------------------------------------------------------
没找到驱动包.检查一下驱动包的配置.
----------------解决方案--------------------------------------------------------
改一下driver
String driver = "org.gjt.mm.mysql.Driver";
----------------解决方案--------------------------------------------------------
2楼的是不是失恋了啊,我心情也不好,哎郁闷。。。。
----------------解决方案--------------------------------------------------------
String driver = "com.mysql.jdbc.driver";

java.lang.ClassNotFoundException: com.mysql.jdbc.driver

信息说是他的问题,要相信他!driver改成Driver
改成
String driver = "com.mysql.jdbc.Driver";类名首字母要大写

----------------解决方案--------------------------------------------------------
把包导入本工程中了吗
----------------解决方案--------------------------------------------------------

谢谢楼上几位,我再调试一下


----------------解决方案--------------------------------------------------------
  相关解决方案