ORM是指:对象和关系数据库的映射,简单说,一个对象,对应数据库里的一条记录。
1.举个例子
代码如下:
package jdbc8;
public class Hero {
public int id;
public String name;
public float hp;
public int damage;
}
package jdbc8;
import java.sql.*;
public class test1 {
public static Hero get(int id) {
Hero hero = null;
Connection c = null;
Statement s = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8","root", "admin");
s = c.createStatement();
String sql = "select * from hero where id = " + id;
rs = s.executeQuery(sql);
if(rs.next()) {
hero = new Hero();
String name = rs.getString(2);
float hp = rs.getFloat("hp");
int damage = rs.getInt(4);
hero.name = name;
hero.hp = hp;
hero.damage = damage;
hero.id = id;
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(rs != null) {
try {
rs.close();
}catch(Exception e) {
e.printStackTrace();
}
rs = null;
}
if(s != null) {
try {
s.close();
}catch(Exception e) {
e.printStackTrace();
}
s = null;
}
if(c != null) {
try {
c.close();