1、打开Access2013
2、单机空白桌面数据库,这里是根据需求来选,我只是想初步了解Access,选择这个就行
3、选择数据库存放位置,并命名,我起名为Test
3、点击工具栏的创建,点击查询设计,会弹出显示表的对话框,关闭这个对话框
4、点击左上角的SQL视图,再点击SQL视图,在打开的面板里可以写SQL语句。
5、创建表,插入数据,查看数据,sql代码同mysql
6、使用java操作access
- 导入Java包。下载Access_JDBC30.jar 提取码: 1347
这个jar包每次查询不能超过50条数据,而且好像只能查询1000次,仅适用于学生学习使用。😂 - 获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
conn = (Connection) DriverManager.getConnection("jdbc:Access:///G:/Access/test.accdb", "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 连接数据库
return conn;
}
3、使用preparedStatement执行sql语句,如果是查询,使用ResultSet接收结果集。这一部分具体操作方法和mysql一模一样如果不懂mysql,见JDBC操作Mysql
4、整体的代码
package access测试;
import java.sql.*;
import javabean.Student;//这个包是我自己定义的,里面写的是Student类
public class Test {
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.hxtt.sql.access.AccessDriver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
conn = (Connection) DriverManager.getConnection("jdbc:Access:///G:/Access/test.accdb", "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 连接数据库
return conn;
}
public static void insert(Student stu) {
Connection conn = getConnection();
PreparedStatement pst = null;
String sql = "insert into student values(?,?,?,?,?)";
// String sql="insert into student values('20182121','msn','女',20,'CS')";
try {
pst = conn.prepareStatement(sql);
pst.setObject(1, stu.getSno());
pst.setObject(2, stu.getSname());
pst.setObject(3, stu.getSsex());
pst.setObject(4, stu.getSage());
pst.setObject(5, stu.getSdept());
pst.executeUpdate();
// Statement st = conn.createStatement();
// st.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pst != null)
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void select() {
Connection conn = getConnection();
PreparedStatement pst = null;
String sql = "select *from student";
try {
pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("sno") + " " + rs.getString("sname") + " " + rs.getString("ssex") + " "
+ rs.getInt("sage") + " " + rs.getString("sdept"));
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (pst != null)
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Student stu=new Student("201212128", "蔡徐坤", "男", 19, "CS");
insert(stu);
select();
}
}
5、如果查询没有问题,插入也没有报错,但是数据库不显示,select查询出的关系表中也没有这条数据,或者再次插入报错Failed to insert a new record into table student:Failed to insert a duplicat
,我被这个问题困扰了好久。我是把数据库关了然后再打开,数据库就显示结果了。