使用java连接数据库 - 驱动程序错误[重复]
Posted
技术标签:
【中文标题】使用java连接数据库 - 驱动程序错误[重复]【英文标题】:Connecting databases with java - driver Error [duplicate] 【发布时间】:2013-05-08 06:33:14 【问题描述】:我正在尝试连接我正在制作的关于我在 MS Access 中创建的大学数据库的 Java 程序。
我的代码是这样的:
package Uni;
import java.util.*;
import java.sql.*;
public class College
static final String database_URL = "H:/CSCI 213/Labs/Lab 11 - 12/src/Uni/Uni.accdb";
static Scanner input = new Scanner(System.in);
static Scanner in = new Scanner(System.in);
@SuppressWarnings("unused")
public static void main(String[] args)
Connection connection = null;
Statement statement = null;
ResultSet resultset = null;
String name;
int age;
int id;
String choice = "y";
while(choice.equalsIgnoreCase("y"))
System.out.println("What Would you like to do");
System.out.println("1- Insert");
System.out.println("2- Update");
System.out.println("3- Search");
System.out.println("4- Delete");
System.out.println("5- Exit");
int x = input.nextInt();
try
connection = DriverManager.getConnection(database_URL);
//connection.commit();
//statement = connection.createStatement();
switch(x)
case 1:
System.out.println("Enter the name:");
name = input.nextLine();
System.out.println("Enter the Age:");
age = input.nextInt();
//statement.executeQuery("INSERT INTO Database VALUES ('"+name+"','"+age+"')");
Statement sta1 = connection.createStatement();
sta1.executeQuery("INSERT INTO Database VALUES ('"+name+"','"+age+"')");
sta1.close();
System.out.println("Your ID is: ");
//statement.executeQuery("select ID from Database where Name='"+name+"';");
Statement sta2 = connection.createStatement();
sta2.executeQuery("select ID from Database where Name='"+name+"';");
connection.commit();
sta2.close();
break;
case 2:
System.out.println("Enter the id of the person you'd like to update:");
id = input.nextInt();
System.out.println("Enter the name:");
name = input.nextLine();
System.out.println("Enter the Age:");
age = input.nextInt();
//statement.executeUpdate("UPDATE Database SET Age="+age+", Name='"+name+"' WHERE ID="+id+";");
Statement sta3 = connection.createStatement();
sta3.executeQuery("UPDATE Database SET Age="+age+", Name='"+name+"' WHERE ID="+id+";");
connection.commit();
sta3.close();
break;
case 3:
System.out.println("Enter the Name of the person you'd like to search for:");
name = input.nextLine();
//statement.executeQuery("select Name, Age from Database where Name='"+name+"';");
Statement sta4 = connection.createStatement();
sta4.executeQuery("select Name, Age from Database where Name='"+name+"';");
connection.commit();
sta4.close();
break;
case 4:
System.out.println("Enter the id of the person you'd like to delete:");
id = input.nextInt();
//statement.execute("DELETE FROM Database where ID='"+id+"';");
Statement sta5 = connection.createStatement();
sta5.executeQuery("DELETE FROM Database where ID='"+id+"';");
connection.commit();
sta5.close();
break;
case 5:
System.out.println("Terminating");
System.exit(0);
default:
System.out.println("Wrong Entry");
catch (SQLException e)
System.out.println("SQLException: "+e.getMessage());
System.out.println("Would you like to try again ? (enter y for yes)");
choice = in.nextLine();
现在确切的输出如下
你想做什么 1-插入 2-更新 3- 搜索 4-删除 5- 退出 1 SQLException: 未找到适用于 H:/CSCI 213/Labs/Lab 11 - 12/src/Uni/Uni.accdb 的驱动程序 你想再试一次吗? (输入 y 表示是)
任何关于如何解决这个问题的想法都非常感谢
【问题讨论】:
【参考方案1】:H:/CSCI 213/Labs/Lab 11 - 12/src/Uni/Uni.accdb
是文件路径,不是有效的 JDBC URL。
您需要为特定数据库获取 JDBC 驱动程序 JAR 文件,在运行程序时将其放入类路径中,并使用正确的 JDBC URL。 JDBC URL 的确切外观取决于您使用的数据库和驱动程序(查看文档)。
有关通过 JDBC 使用 Microsoft Access 数据库,请参阅例如:How to connect Java to MS Access 或搜索“Microsoft Access JDBC 驱动程序”。
有关详细信息,请参阅 Oracle 的 JDBC Getting Started tutorial。
【讨论】:
以上是关于使用java连接数据库 - 驱动程序错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章
servlet 的数据库连接问题 - Java webapp [重复]