package 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();
} } } }

hibernate01ORM的引入-LMLPHP

hibernate01ORM的引入-LMLPHP

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 <=count; i++) {
String name= data.getColumnName(i); //字段名称
String type= data.getColumnTypeName(i); //字段类型 System.out.println(name+"====》"+type);
//给我一个字段名称 ,我给你返回一个set字段名 给类中的属性 赋值
String method=getMethod(name);
//04.判断 数据库中的字段类型 要和java中的数据类型相互匹配
if (type.equals("NUMBER")) {
object.getClass().getMethod(method, Integer.class)
.invoke(object, rs.getInt(name));
}else if(type.equals("VARCHAR2")){
object.getClass().getMethod(method, String.class)
.invoke(object, rs.getString(name));
}
}
Dept dept=(Dept) object;
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();
} } } /**
*给我一个字段名称 ,我给你返回一个set字段名 给类中的属性 赋值
* @param name 字段名称
* @return setName()
*/
private static String getMethod(String name) {
return "set"+name.substring(0, 1).toUpperCase()+name.substring(1).toLowerCase();
} }
05-11 14:03