我正在关注有关将SQLite数据库连接到Java应用程序的教程。

当我运行程序时,在NetBeans控制台中出现以下错误:


跑:

连接数据库时出错java.sql.SQLException:不适合
找到jdbc的驱动程序:C:\ Users \ lawman \ Documents \ Java Working
目录\ LoginSql \ src \ project123.sqlite

建立成功(总时间:0秒)


这是我的目录:

java - 在Java中连接到sqlite数据库的问题-LMLPHP

我有代码可以连接到类tobecalledbymain中的数据库。

我在main中有mainclass,它创建了tobecalledbymain的实例。

在我的库文件中,导入了sqlite-jdbcs.jar

这是tobecalledinmain的代码:

import java.sql.*;

public class tobecalledinmain {

    public tobecalledinmain(){
        Connection con = null;
        Statement st=null;
        ResultSet rs=null;
        try
        {
            Class.forName("org.sqlite.JDBC");
            con = DriverManager.getConnection("jdbc:C:\\Users\\lawman\\Documents\\"
                    + "Java Working Directory\\LoginSql\\"
                    + "src\\project123.sqlite");
            st=con.createStatement();

            //select all records from the table employee
            //table has three firlds: employeeid,name and surname
            rs=st.executeQuery("SELECT * FROM Employee;");
            while (rs.next())
            {
                 int id = rs.getInt("Employeeid");
                 String name = rs.getString("Name");
                 System.out.println("id = " + id);
                 System.out.println("name= " + name);
                 System.out.println();
            }
            rs.close();
            st.close();
            con.close();

        }catch(Exception e)
        {
            System.out.println("Error connecting to the database" + e);
        }

    }

}


这是mainclass代码:

public class mainClass {

    public static void main(String[] args){
        new tobecalledinmain();
    }

}
;;


我不确定为什么我们需要分号!

无论如何,当本教程得出结论时,他将从控制台获得结果。我收到上述错误消息。

错误消息中所指的驱动程序是什么,如何获得它们?

最佳答案

您的jdbc连接字符串未指定sqlite。试试这个,并使用正斜杠。

Connection con = DriverManager.getConnection("jdbc:sqlite:C:/PATH/TO/database.db");

关于java - 在Java中连接到sqlite数据库的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33332639/

10-09 07:32
查看更多