package com.turing;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
//---
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletInputStream;
//---
public class DaoImpl implements IDao{
// private String sDBDriver = "oracle.jdbc.driver.OracleDriver";
// private String sConnStr = "jdbc:oracle:thin:@localhost:1521:bcrd";//设置数据库名称为:pubs
// private String user = "admin"; //登录数据库用户名
// private String password = "admin"; //登录数据库密码
private String sDBDriver = "com.mysql.jdbc.Driver";
private String sConnStr = "jdbc:mysql://localhost:3306/paging";
private String user = "root";
private String password = "bcrd";
/**
* 建立连接
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(sDBDriver); //指定JDBC数据库驱动程序
return DriverManager.getConnection(sConnStr,user,password);
}
/**
* 根据sql查询列表数据(查询一条),不支持预编译的方式
* @param sql
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public Map<String, Object> executeQueryForMap(String sql)throws ClassNotFoundException, SQLException {
Connection connect =this.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<Map<String, Object>> list = this.rsToList(rs);
if( !list.isEmpty() ){
return list.get(0);
}
this.releaseConnection(rs, stmt, connect);//关闭连接
return null;
}
/**
* 根据sql查询列表数据(查询一条),支持预编译的方式
* @param sql
* @param types
* @param ObjectValues
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
public Map<String, Object> executeQueryForMap(String sql, int[] types,Object[] ObjectValues) throws ClassNotFoundException, SQLException, IOException {
Connection connect = this.getConnection();
PreparedStatement pst = (PreparedStatement) connect.prepareStatement(sql);
if( types != null ){
prepareSetValue(types, ObjectValues, pst);
}
ResultSet rs = pst.executeQuery();
List<Map<String, Object>> list = this.rsToList(rs);
if( !list.isEmpty() ){
return list.get(0);
}
this.releaseConnection(rs, pst, connect);
return null;
}
/**
* 预编译传参设置传参
* @param types 参数类型
* @param ObjectValues 参数的具体数据
* @param pst 预编译可执行对象
* @throws SQLException SQL异常
* @throws FileNotFoundException 文件没找到
* @throws IOException IO异常:数据库没连上
*/
private void prepareSetValue(int[] types, Object[] ObjectValues,
PreparedStatement pst) throws SQLException, FileNotFoundException,
IOException {
for(int i=0;i<types.length;i++){
switch( types[i] ){
case Types.VARCHAR:
pst.setString(i+1, String.valueOf( ObjectValues[i] ) );
break;
case Types.INTEGER:
pst.setInt(i+1, Integer.parseInt( String.valueOf( ObjectValues[i] ) ));
break;
case Types.DOUBLE:
pst.setDouble(i+1, Double.parseDouble( String.valueOf( ObjectValues[i] ) ));
break;
case Types.BLOB:
InputStream in = new FileInputStream( (File)ObjectValues[i] );
pst.setBinaryStream(i+1, in , in.available() );
break;
case Types.DATE:
pst.setDate(i+1, new java.sql.Date(((java.util.Date)ObjectValues[i]).getTime()) );
break;
case Types.DECIMAL:
pst.setDouble(i+1, Double.parseDouble(String.valueOf( ObjectValues[i]) ));
break;
case Types.NUMERIC:
pst.setDouble(i+1, Double.parseDouble(String.valueOf( ObjectValues[i]) ));
break;
case Types.FLOAT:
pst.setFloat(i+1, Float.parseFloat(String.valueOf( ObjectValues[i]) ) );
break;
case Types.TIME:
pst.setTime(i+1, new java.sql.Time(((java.util.Date)ObjectValues[i]).getTime()));
break;
}
}
}
/**
* 根据sql查询列表数据,不支持预编译的方式
* @param sql
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public List<Map<String, Object>> executeQueryForList(String sql) throws ClassNotFoundException, SQLException{
Connection connect =this.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<Map<String, Object>> list = this.rsToList(rs);
this.releaseConnection(rs, stmt, connect);//关闭连接
return list;
}
/**
* 执行 增、删、改、等的操作,不支持预编译的方式
* @param sql
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public int executeUpdate(String sql) throws ClassNotFoundException, SQLException {
Connection connect=this.getConnection();
Statement stmt=connect.createStatement();
int count=stmt.executeUpdate(sql);
this.releaseConnection(stmt, connect);//关闭连接
return count;
}
/**
* 根据sql查询列表数据,支持预编译的方式
* @param sql
* @param types
* @param ObjectValues
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
public List<Map<String, Object>> executeQueryForList(String sql , int[] types,Object[] ObjectValues) throws ClassNotFoundException, SQLException, IOException{
Connection connect = this.getConnection();
PreparedStatement pst = connect.prepareStatement(sql);
if( types != null ){
prepareSetValue(types, ObjectValues, pst);
}
ResultSet rs = pst.executeQuery();
List<Map<String, Object>> list = this.rsToList(rs);
this.releaseConnection(rs, pst, connect);
return list;
}
/**
* 预编译sql操作, 支持insert , update , delete 语句
* @param sql
* @param types
* @param ObjectValues
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
public int executeUpdate(String sql , int[] types,Object[] ObjectValues) throws ClassNotFoundException, SQLException, IOException{
Connection connect = this.getConnection();
PreparedStatement pst = connect.prepareStatement(sql);
if( types != null ){
prepareSetValue(types, ObjectValues, pst);
}
int count = pst.executeUpdate();
this.releaseConnection(pst, connect);
return count;
}
/**
* 查询一个整数,例如记录总数(不支持预编译)
* @param sql
* @param types
* @param ObjectValues
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public int executeQueryForCount(String sql) throws ClassNotFoundException, SQLException{
Connection connect =this.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
return rs.getInt(1);
}
this.releaseConnection(rs, stmt, connect);
return 0;
}
/**
* 查询一个整数,例如记录总数(支持预编译)
* @param sql
* @param types
* @param ObjectValues
* @return
* @throws SQLException
* @throws ClassNotFoundException
* @throws IOException
*/
public int executeQueryForCount(String sql , int[] types,Object[] ObjectValues) throws ClassNotFou