简体   繁体   English

使用mysql的JDBC Type 4连接

[英]JDBC type 4 connectivity using mysql

I wrote a program to connect to a database using jdbc with type 4 connectivity. 我编写了一个程序,使用具有类型4连接性的jdbc连接到数据库。 The program compiles just fine but gives an exception of java.lang.ClassNotFoundException : com.mysql.jdbc.Driver. 该程序可以很好地编译,但是给出了java.lang.ClassNotFoundException的异常:com.mysql.jdbc.Driver。 I have extracted all the folders in the folder in which my java file is and even placed all the jar files and database in the same folder. 我已经提取了我的Java文件所在的文件夹中的所有文件夹,甚至将所有的jar文件和数据库都放在了同一文件夹中。 The database contains two records with ID 1 and 2. What could be the problem? 该数据库包含两个ID为1和2的记录。可能是什么问题?

import java.sql.*;

class TestJDBC {

public static void main(String aa[]) {

    try {
        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student_Details", "root","");

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("Select ID, studentname from Details where ID=1");

        while(rs.next()) {
            System.out.print(rs.getString("ID"));
            System.out.print("\t");
            System.out.println(rs.getString("StudentName"));
        }
    }
    catch(Exception e) {
        System.out.println(e);
    }
}

} }

If your mysql-connector-java-5.0.8-bin.jar file is located with a path like \\lib\\ within your Project like below 如果您的mysql-connector-java-5.0.8-bin.jar文件在项目中的路径位于\\lib\\之类,如下所示

在此处输入图片说明

Then with a cmd prompt (assuming windows) change directory to this folder, then you would need to compile your class with 然后使用cmd提示(假设Windows)将目录更改为此文件夹,则需要使用以下命令编译类

javac -cp ".;lib/mysql-connector-java-5.0.8-bin.jar" YourClass.java

use : if not windows (only need quotes if spaces in file/folder names) 使用:如果不是Windows(如果文件/文件夹名称中有空格,则仅需使用引号)

then run with 然后运行

java -cp ".;lib/mysql-connector-java-5.0.8-bin.jar" YourClass

Also make sure you dont have duplicate jars in use 还要确保您没有使用重复的罐子

you need to have the jar containing com.mysql.jdbc.Driver in the class path 您需要在类路径中包含com.mysql.jdbc.Driver的jar

eg 例如

java -cp mysql.jar;. TestJDBC

where mysql.jar is the jar containing the class file 其中mysql.jar是包含类文件的jar

com/mysql/jdbc/Driver.class

on a windows machine 在Windows机器上

placed mysql-connector-java-5.0.8-bin.jar and TestJDBC.class in same folder. mysql-connector-java-5.0.8-bin.jarTestJDBC.class放在同一文件夹中。

from the folder executed java -cp mysql-connector-java-5.0.8-bin.jar;. TestJDBC 从文件夹中执行java -cp mysql-connector-java-5.0.8-bin.jar;. TestJDBC java -cp mysql-connector-java-5.0.8-bin.jar;. TestJDBC

that worked as expected. 如预期般运作。

unix usually requires Unix通常需要

java -cp mysql-connector-java-5.0.8-bin.jar:. TestJDBC

As answered by BevynQ, setting class path from command line sets the path(As best of my knowledge) for that particular current instance of cmd, if you exit/close and reopen command prompt, again you need to set the path. 正如BevynQ回答的那样,从命令行设置类路径将设置该cmd当前特定实例的路径(据我所知),如果退出/关闭并重新打开命令提示符,则再次需要设置路径。 I guess two solutions for this to use jar files in your programs: 我猜想有两种解决方案可以在您的程序中使用jar文件:

  • Solution 1) Copy jar files in Program Files/Java/JRE/lib folder, so that it will be available for run time environment, But this is not recommended to add jar files in JDK/JRE. 解决方案1)将jar文件复制到Program Files / Java / JRE / lib文件夹中,以便可以在运行时环境中使用,但是不建议在JDK / JRE中添加jar文件。

  • Solution 2) Set the classpath variable to place the full path of your jar file location, So that it will be available for run time environment: Copy full path of your jar file location, go to environment variable, search for user/system classpath variable(User variable for current login user, system variable for all users accounts on current windows) if it is there, edit it and put your path in starting, otherwise create a new classpath variable and place your full path 解决方案2)设置classpath变量以放置jar文件位置的完整路径,以便在运行时环境中可用:复制jar文件位置的完整路径,转到环境变量,搜索用户/系统classpath变量(当前登录用户的用户变量,当前窗口上所有用户帐户的系统变量)是否存在,请对其进行编辑并将您的路径置于开始位置,否则创建一个新的classpath变量并放置完整路径

Look here to set class-path- 看这里设置class-path-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM