Java使用ucanaccess连接Access数据库,进行增删改查
需要的Jar包如下:
代码如下:
package com.accord;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Testes {
public static void main(String[] args) throws Exception {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");// 这个驱动的地址不要改
// Connection
// con=DriverManager.getConnection("jdbc:ucanaccess://D:\\Database3.mdb","123456","123456");
Connection con = DriverManager
.getConnection("jdbc:ucanaccess://D:\\add.mdb");
Statement stmt = con.createStatement();
// 查找数据
ResultSet rs = stmt.executeQuery("select * from test where id = 1");
while (rs.next()) {
System.out.println(rs.getString("username"));
}
// 添加数据
if (stmt.executeUpdate("insert into test(username) values('李四')") > 0) {
System.out.println("添加成功!!!");
} else {
System.out.println("添加失败!!!");
}
//修改
if (stmt.executeUpdate("update test set username = '李思思' where id = 3 ") > 0) {
System.out.println("修改成功!!!");
} else {
System.out.println("修改失败!!!");
}
//删除
if (stmt.executeUpdate("delete from test where id = 3 ") > 0) {
System.out.println("删除成功!!!");
} else {
System.out.println("删除失败!!!");
}
}
}
其中:ID是自增的;
更多推荐
Java使用ucanaccess连接Access数据库,进行增删改查
发布评论