h2 数据库驱动程序发出驱动程序未找到消息

the h2 database driver giving out driver not found message

我已经学习 JDBC 连接几天了,最近使用 h2 关系数据库尝试了这段代码。我收到一条消息,因为驱动程序不可用,我已经检查过,驱动程序在库中应该在的位置 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());       
        }

    }
}

使用此代码

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

 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!");
            }
        }

    }

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

//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