Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未找到适合jdbc的驱动程序:mysql://localhost/db_Java_Sql_Jsp_Jdbc - Fatal编程技术网

Java 未找到适合jdbc的驱动程序:mysql://localhost/db

Java 未找到适合jdbc的驱动程序:mysql://localhost/db,java,sql,jsp,jdbc,Java,Sql,Jsp,Jdbc,我是一名java初学者,正在尝试在db中插入一行。这是我在java中第一次执行插入操作。大约两个小时以来,我一直在谷歌上搜索,感到沮丧,无法解决我的错误。我打电话给我的朋友,他在TeamViewer中为我提供了实时支持,只给我的程序添加了一行代码 Class.forName("com.mysql.jdbc.Driver") 任何人都可以解释为什么我们需要在连接字符串之前在我的代码中包含这个。每次都有必要把我的代码放在那里吗。请给我详细解释。是的,每次都必须包括在内。 但是您可以使用一种不重复代

我是一名java初学者,正在尝试在db中插入一行。这是我在java中第一次执行插入操作。大约两个小时以来,我一直在谷歌上搜索,感到沮丧,无法解决我的错误。我打电话给我的朋友,他在TeamViewer中为我提供了实时支持,只给我的程序添加了一行代码

Class.forName("com.mysql.jdbc.Driver")

任何人都可以解释为什么我们需要在连接字符串之前在我的代码中包含这个。每次都有必要把我的代码放在那里吗。请给我详细解释。

是的,每次都必须包括在内。 但是您可以使用一种不重复代码的方法

比如说

public void connectToMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase","username",""password);
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
public void insert(String sql)
{
connectToMYSQL();
//.. then do your stuffs 

}
}

在编写任何sql语句之前,只需调用该方法即可

public void connectToMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase","username",""password);
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
public void insert(String sql)
{
connectToMYSQL();
//.. then do your stuffs 

}

是的,每次都必须包括在内。 但是您可以使用一种不重复代码的方法

比如说

public void connectToMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase","username",""password);
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
public void insert(String sql)
{
connectToMYSQL();
//.. then do your stuffs 

}
}

在编写任何sql语句之前,只需调用该方法即可

public void connectToMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase","username",""password);
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
public void insert(String sql)
{
connectToMYSQL();
//.. then do your stuffs 

}
Class.forName(“com.mysql.jdbc.Driver”)

必须加载正在使用的数据库的驱动程序。 此行中的forNmae()方法加载mysql数据库的驱动程序。

Class.forName(“com.mysql.jdbc.driver”)

必须加载正在使用的数据库的驱动程序。
此行中的forNmae()方法加载mysql数据库的驱动程序。

基本思想是,此操作强制在JDBC的驱动程序管理器中注册驱动程序类


方法Class.forName(“完全限定类名”)用于初始化类的静态字段,并将JDBC驱动程序类(本例中为MySQL驱动程序)加载到应用程序中。实例化后,它将注册到DriverManager。通过后者,您可以使用Connection Connection=DriverManager.getConnection(“JDBC:mysql://localhost:3306/databasename“,”登录“,”密码“;,您稍后将使用它来查询数据库。

基本思想是,此操作强制在JDBC的驱动程序管理器中注册驱动程序类


方法Class.forName(“完全限定类名”)用于初始化类的静态字段,并将JDBC驱动程序类(本例中为MySQL驱动程序)加载到应用程序中。实例化后,它将注册到DriverManager。通过后者,您可以使用Connection Connection=DriverManager.getConnection(“JDBC:mysql://localhost:3306/databasename“,”登录“,”密码“;,稍后您将使用它来查询数据库。

以下是一些非常简化的代码,说明了驱动程序初始化的工作原理。共有3个类,请将每个类放入一个适当命名的文件中

import java.util.HashMap;
import java.util.Map;

public class DriverMgr {
    private static final Map<String, Class<?>> DRIVER_MAP = new HashMap<String, Class<?>>();

    public static void registerDriver(final String name, final Class<?> cls) {
        DRIVER_MAP.put(name, cls);
    }

    public static Object getDriver(final String name) {
        final Class<?> cls = DRIVER_MAP.get(name);
        if (cls == null) {
            throw new RuntimeException("Driver for " + name + " not found");
        }
        try {
            return cls.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Driver instantiation failed", e);
        }
    }
}

public class MysqlDriver {
    static {
        // hello, I am a static initializer
        DriverMgr.registerDriver("mysql", MysqlDriver.class);
    }

    @Override
    public String toString() {
        return "I am the mysql driver";
    }
}

public class TestProg {
    public static void main(final String... args) {
        try {
            Class.forName("MysqlDriver"); // try with or without this
        } catch (Exception e) {
            throw new RuntimeException("Oops, failed to initialize the driver");
        }
        System.out.println(DriverMgr.getDriver("mysql"));
    }
}
import java.util.HashMap;
导入java.util.Map;
公共类驱动程序{
私有静态最终映射>();
公共静态void registerDriver(最终字符串名称,最终类cls){
驾驶员地图放置(名称,cls);
}
公共静态对象getDriver(最终字符串名称){
最终类cls=驱动程序\映射.get(名称);
如果(cls==null){
抛出新的RuntimeException(“未找到“+name+”的驱动程序”);
}
试一试{
返回cls.newInstance();
}捕获(例外e){
抛出新的RuntimeException(“驱动程序实例化失败”,e);
}
}
}
公共类MysqlDriver{
静止的{
//你好,我是一个静态初始化器
registerDriver(“mysql”,MysqlDriver.class);
}
@凌驾
公共字符串toString(){
返回“我是mysql驱动程序”;
}
}
公共类测试程序{
公共静态void main(最终字符串…参数){
试一试{
Class.forName(“MysqlDriver”);//尝试使用或不使用此
}捕获(例外e){
抛出新的RuntimeException(“Oops,未能初始化驱动程序”);
}
System.out.println(DriverMgr.getDriver(“mysql”);
}
}

调用Class.forName时,将加载驱动程序类并执行静态初始值设定项。这反过来会向驱动程序管理器注册驱动程序类,以便管理器现在知道它。显然,这只需要执行一次。

下面是一些非常简化的代码,说明了驱动程序初始化的工作原理。共有3个类,请将每个类放入一个适当命名的文件中

import java.util.HashMap;
import java.util.Map;

public class DriverMgr {
    private static final Map<String, Class<?>> DRIVER_MAP = new HashMap<String, Class<?>>();

    public static void registerDriver(final String name, final Class<?> cls) {
        DRIVER_MAP.put(name, cls);
    }

    public static Object getDriver(final String name) {
        final Class<?> cls = DRIVER_MAP.get(name);
        if (cls == null) {
            throw new RuntimeException("Driver for " + name + " not found");
        }
        try {
            return cls.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Driver instantiation failed", e);
        }
    }
}

public class MysqlDriver {
    static {
        // hello, I am a static initializer
        DriverMgr.registerDriver("mysql", MysqlDriver.class);
    }

    @Override
    public String toString() {
        return "I am the mysql driver";
    }
}

public class TestProg {
    public static void main(final String... args) {
        try {
            Class.forName("MysqlDriver"); // try with or without this
        } catch (Exception e) {
            throw new RuntimeException("Oops, failed to initialize the driver");
        }
        System.out.println(DriverMgr.getDriver("mysql"));
    }
}
import java.util.HashMap;
导入java.util.Map;
公共类驱动程序{
私有静态最终映射>();
公共静态void registerDriver(最终字符串名称,最终类cls){
驾驶员地图放置(名称,cls);
}
公共静态对象getDriver(最终字符串名称){
最终类cls=驱动程序\映射.get(名称);
如果(cls==null){
抛出新的RuntimeException(“未找到“+name+”的驱动程序”);
}
试一试{
返回cls.newInstance();
}捕获(例外e){
抛出新的RuntimeException(“驱动程序实例化失败”,e);
}
}
}
公共类MysqlDriver{
静止的{
//你好,我是一个静态初始化器
registerDriver(“mysql”,MysqlDriver.class);
}
@凌驾
公共字符串toString(){
返回“我是mysql驱动程序”;
}
}
公共类测试程序{
公共静态void main(最终字符串…参数){
试一试{
Class.forName(“MysqlDriver”);//尝试使用或不使用此
}捕获(例外e){
抛出新的RuntimeException(“Oops,未能初始化驱动程序”);
}
System.out.println(DriverMgr.getDriver(“mysql”);
}
}

调用Class.forName时,将加载驱动程序类并执行静态初始值设定项。这反过来会向驱动程序管理器注册驱动程序类,以便管理器现在知道它。显然,这只需要做一次。

可能会有所帮助。请看它回答了您的问题,您使用的是哪个JRE版本?如果您的代码编写正确,您只需要做一次。将方法分解为更小的