当前位置: 技术问答>java相关
在JSP中使用mm.mysql.jdbc-2.0pre5连接mysql数据库的问题!!急急急!·#·¥#!!!在线给分!!!!!!
来源: 互联网 发布时间:2015-09-22
本文导语: MYSQL、JSDK、JDBC……都装在C盘,关于JDBC的环境变量也设为: c:mm.mysql.jdbc-2.0pre5; c:mm.mysql.jdbc-2.0pre5mysql_2_comp.jar; c:mm.mysql.jdbc-2.0pre5mysql_2_uncomp.jar; 使用如下代码测试连接: import java.sql.*; public class mysql { public static vo...
MYSQL、JSDK、JDBC……都装在C盘,关于JDBC的环境变量也设为:
c:mm.mysql.jdbc-2.0pre5;
c:mm.mysql.jdbc-2.0pre5mysql_2_comp.jar;
c:mm.mysql.jdbc-2.0pre5mysql_2_uncomp.jar;
使用如下代码测试连接:
import java.sql.*;
public class mysql
{
public static void main(String args[])
{
String mysqlDriver="org.git.mm.mysql.Driver";
try
{ Class.forName(mysqlDriver);
System.out.println("驱动接口程序连接成功!!!");
}
catch(Exception e)
{
System.out.println("驱动接口程序加载失败!!");
e.printStackTrace();
}
}
}
在JBUILDER7中总报出如下错误:
D:JBuilder7jdk1.3.1binjavaw -classpath "C:Apache Tomcat 4.0webappsROOTWEB-INFclasses;D:JBuilder7jakarta-tomcat-4.0.3commonlibservlet.jar;D:JBuilder7libbeansdxejb.jar;D:JBuilder7libdx.jar;D:JBuilder7libbeandt.jar;D:JBuilder7jdk1.3.1demojfcJava2DJava2Demo.jar;D:JBuilder7jdk1.3.1jrelibi18n.jar;D:JBuilder7jdk1.3.1jrelibjaws.jar;D:JBuilder7jdk1.3.1jrelibrt.jar;D:JBuilder7jdk1.3.1jrelibsunrsasign.jar;D:JBuilder7jdk1.3.1libdt.jar;D:JBuilder7jdk1.3.1libhtmlconverter.jar;D:JBuilder7jdk1.3.1libtools.jar" mysql
java.lang.ClassNotFoundException: org.git.mm.mysql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)驱动接口程序加载失败!!
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at mysql.main(mysql.java:8)
哪位老兄帮我??????????
救命啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
c:mm.mysql.jdbc-2.0pre5;
c:mm.mysql.jdbc-2.0pre5mysql_2_comp.jar;
c:mm.mysql.jdbc-2.0pre5mysql_2_uncomp.jar;
使用如下代码测试连接:
import java.sql.*;
public class mysql
{
public static void main(String args[])
{
String mysqlDriver="org.git.mm.mysql.Driver";
try
{ Class.forName(mysqlDriver);
System.out.println("驱动接口程序连接成功!!!");
}
catch(Exception e)
{
System.out.println("驱动接口程序加载失败!!");
e.printStackTrace();
}
}
}
在JBUILDER7中总报出如下错误:
D:JBuilder7jdk1.3.1binjavaw -classpath "C:Apache Tomcat 4.0webappsROOTWEB-INFclasses;D:JBuilder7jakarta-tomcat-4.0.3commonlibservlet.jar;D:JBuilder7libbeansdxejb.jar;D:JBuilder7libdx.jar;D:JBuilder7libbeandt.jar;D:JBuilder7jdk1.3.1demojfcJava2DJava2Demo.jar;D:JBuilder7jdk1.3.1jrelibi18n.jar;D:JBuilder7jdk1.3.1jrelibjaws.jar;D:JBuilder7jdk1.3.1jrelibrt.jar;D:JBuilder7jdk1.3.1jrelibsunrsasign.jar;D:JBuilder7jdk1.3.1libdt.jar;D:JBuilder7jdk1.3.1libhtmlconverter.jar;D:JBuilder7jdk1.3.1libtools.jar" mysql
java.lang.ClassNotFoundException: org.git.mm.mysql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)驱动接口程序加载失败!!
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at mysql.main(mysql.java:8)
哪位老兄帮我??????????
救命啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
|
你的jar不对,我用mm.mysql-2.0.4-bin.jar,你的java文件可以运行,
而且你犯了致命的错误
没有import你的class文件
import java.sql.*;
import org.git.mm.mysql.Driver;//你要设好你的classpath,才能找到mm.mysql-2.0.4-bin.jar文件
public class mysql{
。。。。。。。
Class.forName(mysqlDriver);
。。。。。。。。。。
而且你犯了致命的错误
没有import你的class文件
import java.sql.*;
import org.git.mm.mysql.Driver;//你要设好你的classpath,才能找到mm.mysql-2.0.4-bin.jar文件
public class mysql{
。。。。。。。
Class.forName(mysqlDriver);
。。。。。。。。。。
|
java.lang.ClassNotFoundException: org.git.mm.mysql.Driver
说明是classpath设置的问题。
说明是classpath设置的问题。
|
试试这个:
package works;//把这个java文件和编译成的class文件放在web_inf/class/works这个文件夹下
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbConnection{
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
public DbConnection(){
}
/************************************************
从配置文件"db.txt"中读入信息,打开与数据库的链接
************************************************/
public boolean openConnection(){
Properties prop = new Properties();
try{
InputStream is = getClass().getResourceAsStream("db.txt");
prop.load(is);
if(is != null) is.close();
}
catch(IOException e){
System.out.println("[DbConnection] 打开文件db.txt错误!");
}
String jdbc = prop.getProperty("driver");
String uri = prop.getProperty("url");
try{
Class.forName(jdbc).newInstance();
}
catch(ClassNotFoundException e){
System.out.println("JDBC login, Error!@" + e.getMessage());
return false;
}
catch (Exception e){
System.err.println("不能加载JDBC驱动程序!");
e.printStackTrace();
}
try{
this.conn = DriverManager.getConnection (uri,"","");
}
catch(SQLException e){
System.out.println("Generate Connection, Error!" + e.getMessage());
System.out.println("Here is error");
return false;
}
return true;
}
/*************************************************
* executeQuery and executeUpdate * query and update DB
*************************************************/
public ResultSet executeQuery(String query) throws SQLException{
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
return rset;
}
public void executeUpdate(String query) throws SQLException{
stmt = conn.createStatement();
stmt.executeUpdate(query);
if(stmt != null) stmt.close();
}
public void close() throws SQLException{
if(conn != null) conn.close();
if(rset != null) rset.close();
if(stmt != null) stmt.close();
}
protected void finalize() throws Throwable{
close();
}
public static void main(String[] args){
//Design for test purpose.
DbConnection dc = new DbConnection();
System.out.println(dc.openConnection());
}
}
这是一个java文件,需编译成class文件以后使用,db.txt文件如下:
driver=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/worksys
注:worksys为数据库名称。
引用如下:
InBean.openConnection();
nBean.executeUpdate(sql);
InBean.close();
package works;//把这个java文件和编译成的class文件放在web_inf/class/works这个文件夹下
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbConnection{
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
public DbConnection(){
}
/************************************************
从配置文件"db.txt"中读入信息,打开与数据库的链接
************************************************/
public boolean openConnection(){
Properties prop = new Properties();
try{
InputStream is = getClass().getResourceAsStream("db.txt");
prop.load(is);
if(is != null) is.close();
}
catch(IOException e){
System.out.println("[DbConnection] 打开文件db.txt错误!");
}
String jdbc = prop.getProperty("driver");
String uri = prop.getProperty("url");
try{
Class.forName(jdbc).newInstance();
}
catch(ClassNotFoundException e){
System.out.println("JDBC login, Error!@" + e.getMessage());
return false;
}
catch (Exception e){
System.err.println("不能加载JDBC驱动程序!");
e.printStackTrace();
}
try{
this.conn = DriverManager.getConnection (uri,"","");
}
catch(SQLException e){
System.out.println("Generate Connection, Error!" + e.getMessage());
System.out.println("Here is error");
return false;
}
return true;
}
/*************************************************
* executeQuery and executeUpdate * query and update DB
*************************************************/
public ResultSet executeQuery(String query) throws SQLException{
stmt = conn.createStatement();
rset = stmt.executeQuery(query);
return rset;
}
public void executeUpdate(String query) throws SQLException{
stmt = conn.createStatement();
stmt.executeUpdate(query);
if(stmt != null) stmt.close();
}
public void close() throws SQLException{
if(conn != null) conn.close();
if(rset != null) rset.close();
if(stmt != null) stmt.close();
}
protected void finalize() throws Throwable{
close();
}
public static void main(String[] args){
//Design for test purpose.
DbConnection dc = new DbConnection();
System.out.println(dc.openConnection());
}
}
这是一个java文件,需编译成class文件以后使用,db.txt文件如下:
driver=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost:3306/worksys
注:worksys为数据库名称。
引用如下:
InBean.openConnection();
nBean.executeUpdate(sql);
InBean.close();
|
把MYSQL的JAR文件放入D:JBuilder7jakarta-tomcat-4.0.3commonlib裡面,並加入環境變量CLASSPATH裡面
|
我是把mysql_comp.jar,mysql_uncomp.jar文件复制到C:Program FilesApache Tomcat 4.0lib文件夹下