【问题标题】:the h2 database driver giving out driver not found messageh2 数据库驱动程序发出未找到驱动程序的消息
【发布时间】:2016-10-18 08:25:52
【问题描述】:

我这几天一直在学习 JDBC 连接,最近使用 h2 关系数据库尝试了这段代码。我收到一条消息,因为驱动程序不可用,我已经检查过,驱动程序在应该在 lib 中的位置 eclipse包的文件夹。我大概应该怎么做?

package jdbcwork;

//importing java commands
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Manager1 
{
//main method with exception throw
public static void main(String[] args) throws ClassNotFoundException    
    {
    //try block
    try
        {

    //calling the h2 database driver org.h2.Driver
    Class.forName("org.h2.Driver");

    //declaring host ,username and password 
    String host="jdbc:h2:tcp://localhost/~/test/INFORMATION_SCHEMA.COLLATIONS";
    String uName="sa";
    String uPass="sa";

    //giving a connection to the h2 database driver driver 
    Connection conn = DriverManager.getConnection("h2","sa","sa");

        }
    //catch block
    catch(SQLException err)
        {
        //printing an error message
        System.out.println(err.getMessage());       
        }

    }
}

【问题讨论】:

  • 您是否将 H2 驱动程序 jar 添加到应用程序的类路径中?另外请发布exact异常堆栈跟踪,不要只描述错误信息。

标签: java eclipse jdbc h2


【解决方案1】:
//declaring host ,username and password 
    String host="jdbc:h2:tcp://localhost/~/test/INFORMATION_SCHEMA.COLLATIONS";
    String uName="sa";
    String uPass="sa";

    //giving a connection to the h2 database driver driver 
    Connection conn = DriverManager.getConnection("h2","sa","sa");

改成:

//declaring host ,username and password 
    String host="jdbc:h2:tcp://localhost/~/test/INFORMATION_SCHEMA.COLLATIONS";
    String uName="sa";
    String uPass="sa";

    //giving a connection to the h2 database driver driver 
    Connection conn = DriverManager.getConnection(host,uName,uPass);

并确保 h2.jar 必须进入你的类路径

如果要下载,请检查:

http://www.java2s.com/Code/Jar/h/Downloadh2jar.htm

【讨论】:

    【解决方案2】:

    使用此代码

    使用您的数据库名称和所有

     import java.sql.DriverManager;
        import java.sql.Connection;
        import java.sql.SQLException;
    
        public class OracleJDBC {
    
            public static void main(String[] argv) {
    
                System.out.println("-------- Oracle JDBC Connection Testing ------");
    
                try {
    
                    Class.forName("oracle.jdbc.driver.OracleDriver");
    
                } catch (ClassNotFoundException e) {
    
                    System.out.println("Where is your Oracle JDBC Driver?");
                    e.printStackTrace();
                    return;
    
                }
    
                System.out.println("Oracle JDBC Driver Registered!");
    
                Connection connection = null;
    
                try {
    
                    connection = DriverManager.getConnection(
                            "jdbc:oracle:thin:@localhost:1521:xe","system","murali123");
    
                } catch (SQLException e) {
    
                    System.out.println("Connection Failed! Check output console");
                    e.printStackTrace();
                    return;
    
                }
    
                if (connection != null) {
                    System.out.println("You made it, take control your database now!");
                } else {
                    System.out.println("Failed to make connection!");
                }
            }
    
        }
    

    在这里你会知道问题出在哪里。试试这个告诉我你得到的输出。

    【讨论】:

    • op 只需要在classpath中添加h2驱动jar即可。你的回答 oracle db connection 没有意义。 @Jay 答案是正确的,请检查一下。
    • @KetanG 我在钦奈!!
    • 备注,数据库用户名也可以是username="sa"password=""(空字符串)
    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2021-06-12
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多