package com.yanshu.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.autoconfigure.session.SessionProperties.Jdbc;
import com.yanshu.pojo.Emp;
public class Test {
public static void setUpdate(PreparedStatement psta,Object [] par)
{
if(par==null) return;
try {
for(int i=0;i<par.length;i++)
{
psta.setObject(i+1, par[i]);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Emp getEmp(String sql,Object...objects)
{
Emp emp=null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
conn=JdbcUtil.getConn();
try {
ps=conn.prepareStatement(sql);
for(int i=0;i<objects.length;i++)
{
ps.setObject(i+1, objects[i]);
}
rs=ps.executeQuery();
while(rs.next())
{
//1种
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(rs.getInt("id")+" "
+rs.getString("name"));
/**
*结果
* 38 刘瑞光
39 刘瑞光
40 刘瑞光1
41 tom
42 2
*/
//2中实体对象
/*emp=new Emp();
emp.setName(rs.getString("name"));
emp.setId(rs.getInt("id"));
System.out.println(emp);*/
/**
* 结果
* Emp [id=38, name=刘瑞光, hobas=null]
Emp [id=39, name=刘瑞光, hobas=null]
Emp [id=40, name=刘瑞光1, hobas=null]
Emp [id=41, name=tom, hobas=null]
Emp [id=42, name=2, hobas=null]
*/
//3这地可以直接调用实体对象是构造器
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
JdbcUtil.closeConn(rs, null, ps, ps, conn);
}
return emp;
}
public static void main(String[] args) {
String sql="select name,id from emp";
getEmp(sql);
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.autoconfigure.session.SessionProperties.Jdbc;
import com.yanshu.pojo.Emp;
public class Test {
public static void setUpdate(PreparedStatement psta,Object [] par)
{
if(par==null) return;
try {
for(int i=0;i<par.length;i++)
{
psta.setObject(i+1, par[i]);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Emp getEmp(String sql,Object...objects)
{
Emp emp=null;
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
conn=JdbcUtil.getConn();
try {
ps=conn.prepareStatement(sql);
for(int i=0;i<objects.length;i++)
{
ps.setObject(i+1, objects[i]);
}
rs=ps.executeQuery();
while(rs.next())
{
//1种
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(rs.getInt("id")+" "
+rs.getString("name"));
/**
*结果
* 38 刘瑞光
39 刘瑞光
40 刘瑞光1
41 tom
42 2
*/
//2中实体对象
/*emp=new Emp();
emp.setName(rs.getString("name"));
emp.setId(rs.getInt("id"));
System.out.println(emp);*/
/**
* 结果
* Emp [id=38, name=刘瑞光, hobas=null]
Emp [id=39, name=刘瑞光, hobas=null]
Emp [id=40, name=刘瑞光1, hobas=null]
Emp [id=41, name=tom, hobas=null]
Emp [id=42, name=2, hobas=null]
*/
//3这地可以直接调用实体对象是构造器
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
JdbcUtil.closeConn(rs, null, ps, ps, conn);
}
return emp;
}
public static void main(String[] args) {
String sql="select name,id from emp";
getEmp(sql);
}
}