从MYSQL到hibernate的浅谈
在文章的开篇,我想简单的先介绍一下数据库,数据库有三大类数据结构模型,层次结构模型,网状结构模型,关系结构模型,其中最常使用的就是关系结构型数据库。对于关系型数据库而言,我们可以看见最常用的就是Mysql、sqlserver和oracle,下面重点介绍mysql的连接以及使用方式。
##MYsql
数据库的语言分为三类,
1.数据操纵语言DML(Data Manipulation Language)
包括常用的select、insert、update、delete。(Select也可再分为一类,称为数据查询语言)
1.insert插入
1 |
|
private static Connection getConn() {
String driver = “com.mysql.jdbc.Driver”;
String url = “jdbc:mysql://localhost:3306/samp_db”;
String username = “root”;
String password = “”;
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#####插入
private static int insert(Student student) {
Connection conn = getConn();
int i = 0;
String sql = "insert into students (Name,Sex,Age) values(?,?,?)";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setString(3, student.getAge());
i = pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
##### 更新(修改)
private static int update(Student student) {
Connection conn = getConn();
int i = 0;
String sql = “update students set Age=’” + student.getAge() + “‘ where Name=’” + student.getName() + “‘“;
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println(“resutl: “ + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}1
2
##### 查询
private static Integer getAll() {
Connection conn = getConn();
String sql = “select * from students”;
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement)conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println(“============================”);
while (rs.next()) {
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + “\t”);
if ((i == 2) && (rs.getString(i).length() < 8)) {
System.out.print(“\t”);
}
}
System.out.println(“”);
}
System.out.println(“============================”);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}1
2
##### 删除
private static int delete(String name) {
Connection conn = getConn();
int i = 0;
String sql = “delete from students where Name=’” + name + “‘“;
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
i = pstmt.executeUpdate();
System.out.println(“resutl: “ + i);
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}`
关于hibernate(ORM-对象关系映射框架)的介绍,其实hibernate这个框架在我眼里看来,就是一个打包好的JDBC,使用起来会比JDBC简单的多。我们可以在面向对象的思想下,使用java语言完成对数据库的操作,减少了使用关系型数据库SQL语言的操作。
hibernate框架介绍

在做hibernate项目之前,首先需要我们导入hibernate的包跟JDBC的包,具体操作
右键项目名称-properties-Java build path-add external JARs,其余步骤:
1.写hibernate配置文件hibernate.cfg.xml
2.写好一个javabean,并生成其映射文件.hbm.xml。
3.将映射文件写入配置文件中。
4.写测试类。
关于测试类,我们重点需要写好:
配置信息: Configuration configuration = new Configuration().configure();
sessionFactory:SessionFactory sessionFactory = configuration.buildSessionFactory();
session: Session session = sessionFactory.openSession();
事务开启: session.beginTransaction();
事务操作(包括增删改查):
事务保存: session.save(user);
事务提交: session.getTransaction().commit();
关闭: session.close();
`sessionFactory.close();`
关于hibernate的介绍也基本上说完了