码迷,mamicode.com
首页 > 数据库 > 详细

JDBC查询和mysql连接

时间:2015-01-05 01:47:48      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

package lianxi1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;



public class JdbcTest {
@Test
  public void testMysql(){
      Student stu = new Student();
      String sql = "SELECT flowId,type  FROM student where flowId = ?";   // 别名不能使用,?
      stu = get(Student.class,sql,3);
      System.out.println(stu);

  }
  public <T>T get(Class<T> clazz,String sql,Object...args){
      T entity = null;
      Connection conn = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      try {
        conn = JdbcTools.getConnection();
        ps = conn.prepareStatement(sql);
        for(int i=0;i<args.length;i++){
          ps.setObject(i+1, args[i]);
        }
        rs = ps.executeQuery();
        ResultSetMetaData rsma = rs.getMetaData();
        Map<String,Object> map = new HashMap();
        //处理结果集,利用ResultSetMapData结果集填充Map对象,因为结果集的列的别名与属性名称对应
        while(rs.next()){
            for(int i=0;i<rsma.getColumnCount();i++){
                String ColumnName = rsma.getColumnName(i+1);
                Object ColumnValue = rs.getObject(i+1);
                map.put(ColumnName, ColumnValue);
            }            
        }
        //利用反射创建运行时类的对象
        if(map.size()>0){
            entity = clazz.newInstance();
        //遍历Map类的对象,为Class对象的属性赋值
        for(Map.Entry<String,Object> entry : map.entrySet()){
            String fieldName = entry.getKey();
            //System.out.println(fieldName);
            Object fieldValue = entry.getValue();
            //System.out.println(fieldValue);
            ReflectionUtils.setFieldValue(entity, fieldName, fieldValue);
        }
        
        }
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        JdbcTools.release(rs, ps, conn);
    }
      
    return entity;
      
  }
}
package lianxi1;

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.util.Properties;


public class JdbcTools {
    // 数据库连接
    public static Connection getConnection() throws Exception{
        // 1在配置文件中设置连接数据库的四个字符串
        // 2 读取类路径下的jdbc.properites文件
        InputStream is = JdbcTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties pro = new Properties();
        pro.load(is);
        String driver = pro.getProperty("driverClass");
        String url = pro.getProperty("url");
        String user = pro.getProperty("user");
        String password = pro.getProperty("password");
        //3 创建连接
        Class.forName(driver);
        return DriverManager.getConnection(url, user,password);    
    }
    // 执行sql语句
    public static void update(String sql,Object...args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcTools.getConnection();
            ps = conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                 ps.setObject(i+1,args[i]);
            }
            ps.executeUpdate();
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            JdbcTools.release(null,ps, conn);
        }
    }
    
    
    //数据库断开
    public static void release(ResultSet rs,Statement stat,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
}

JDBC查询和mysql连接

标签:

原文地址:http://www.cnblogs.com/yjtm53/p/4202567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!