hibernate01ORM的引入
2021-06-21 15:04
标签:span var pac err except tin .class 连接 多少 hibernate01ORM的引入 标签:span var pac err except tin .class 连接 多少 原文地址:http://www.cnblogs.com/HHR-SUN/p/7181886.htmlpackage cn.bdqn.bean;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Dept(Integer deptNo, String dname, String loc) {
super();
this.deptno = deptNo;
this.dname = dname;
this.loc = loc;
}
public Dept() {
super();
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptNo) {
this.deptno = deptNo;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptNo=" + deptno + ", dname=" + dname + ", loc=" + loc
+ "]";
}
}
package cn.bdqn.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.bdqn.bean.Dept;
public class JDBCTest {
public static void main(String[] args) {
/**
* 获取数据库连接的4要素
* 01.url
* 02.driverClass
* 03.userName
* 04.password
*/
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String userName="t10";
String password="t10";
/**
* 创建jdbc需要的API
*/
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(driver); //加载驱动
connection= DriverManager.getConnection(url, userName, password);//获取连接
String sql="select * from dept where deptno=?"; //书写sql语句
ps=connection.prepareStatement(sql); //实例化ps对象
ps.setInt(1, 30);//给参数赋值
rs=ps.executeQuery(); //获取查询的结果集
while(rs.next()){
int deptno=rs.getInt("deptno");
String dName=rs.getString("dname");
String loc=rs.getString("loc");
Dept dept=new Dept(deptno, dName, loc);
System.out.println(dept);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
//释放资源
rs.close();
ps.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package cn.bdqn.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import cn.bdqn.bean.Dept;
public class ORMTest {
/**
* 元数据:
* Student
* age
* name
* address
* 这些属性都是用来描述Student对象特征的! 描述数据的数据就是元数据!
*
*/
public static void main(String[] args) throws Exception, IllegalAccessException, ClassNotFoundException {
//我们所写的实体类
String className="cn.bdqn.bean.Dept";
//通过反射获取这个类的实例
Object object=Class.forName(className).newInstance();
String driver="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String userName="t10";
String password="t10";
/**
* 创建jdbc需要的API
*/
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(driver); //加载驱动
connection= DriverManager.getConnection(url, userName, password);//获取连接
String sql="select * from dept where deptno=?"; //书写sql语句
ps=connection.prepareStatement(sql); //实例化ps对象
ps.setInt(1, 20);//给参数赋值
rs=ps.executeQuery(); //获取查询的结果集
while(rs.next()){
//01.获取元数据的结果集
ResultSetMetaData data = rs.getMetaData();
//02.获取数据库中有多少字段
int count = data.getColumnCount();
//03.循环获取字段的名称和类型
for (int i = 1; i
下一篇:Hibernate入门