OneWayOrAnother
2015-01-27 11:05
采纳率: 30%
浏览 1.5k

【JAVA JDBC 】【新人】Help!Help!!!!!!!!

private static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String URL="jdbc:sqlserver://localhost:1433;Database=NewsManagerSystem";

private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;

private void disposeResultSet(ResultSet rs){
    try{
        if(rs!=null){
            rs.close();
        }
    }catch(SQLException e){
        e.printStackTrace();
    }
}
private void disposePstmt(PreparedStatement pstmt){
    try{
        if(pstmt!=null){
            pstmt.close();
        }
    }catch(SQLException e){
        e.printStackTrace();
    }
}
private void disposeConn(Connection conn){
    try {
        if(conn!=null)
            conn.close();
    } catch (SQLException e) {      
        e.printStackTrace();
    }
}
private void getConnection()throws SQLException{
    try{
        Class.forName(DRIVER);
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
    conn= DriverManager.getConnection(URL,"Windy_World","Windy_World");
}
@Override
public Object createEntity(Class<?> c,ResultSet rs)
        throws ClassNotFoundException, SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Object obj= c.newInstance();
        try{
            ResultSetMetaData metaData=rs.getMetaData();
            for(int i=1;i<=metaData.getColumnCount();i++){          
                String methodName=String.format("set%s",metaData.getColumnName(i));
                Object value=rs.getObject(i);
                Class<?> paramType=Class.forName(metaData.getColumnClassName(i));
                invokingSetters(obj,methodName,value,paramType);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    return obj;

}
private void invokingSetters(Object obj,String targetMethod,Object values,Class<?> parameterTypes)
        throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
    //System.out.println(targetMethod+"-----"+parameterTypes);
    Method m=obj.getClass().getMethod(targetMethod, parameterTypes);
    m.invoke(obj, values);
}
public <T> Object querySingle(Class<T> c,String sqlStr,Object... params) 
        throws SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException{
    Object returnValue=null;
    try{
        this.getConnection();
        pstmt=conn.prepareStatement(sqlStr);

        if(params!=null){
            for(int i=0;i<params.length;i++){
                pstmt.setObject(i+1, params[i]);
            }
        }
        rs= pstmt.executeQuery();
        if(rs.next()){
            returnValue= createEntity(null, rs);
        }
    }catch(SQLException e){
        e.printStackTrace();
    }finally{
        if(rs!=null)
            disposeResultSet(rs);
        if(pstmt!=null)
            disposePstmt(pstmt);
        if(conn!=null)
            disposeConn(conn);
    }
    return returnValue;
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • OneWayOrAnother 2015-01-27 11:08

    在querySingle() 中指定Class 有点不太好。。。怎样写更好呢??

    打赏 评论

相关推荐 更多相似问题