package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import Print.PrintBean;
public class PrintModel {
private Connection con; //连接语句
private PreparedStatement ps; //预处理语句
private ResultSet rs; //查询结果集
public List getStyles()
{
List printlist=new ArrayList();
/*
* JNDI用法
*/
try {
Context ic=new InitialContext();
DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/Print");
con=ds.getConnection();
ps=con.prepareStatement("select * from shapeInfo");
rs =ps.executeQuery();
while(rs.next())
{
PrintBean pb=new PrintBean();
pb.setShapeID(rs.getString("shapeID"));
pb.setShapeStyle(rs.getString("shapeStyle"));
printlist.add(pb);
}
} catch (NamingException e) {
e.printStackTrace();
System.out.println(e);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}
return printlist;
}
public static void CloseConnction(Connection con)
{
try {
if(con !=null && !con.isClosed())
{
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}
}
/*
* 关闭结果集的方法
*/
public static void CloseResultSet(ResultSet res)
{
try {
if(res !=null)
{
res.close();
res=null;
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}
}
/*
* 关闭SQL语句的方法
*/
public static void CloseStatement(PreparedStatement pstate)
{
try {
if(pstate !=null)
{
pstate.close();
pstate.close();
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}
}
}