jdbc

来源:互联网 发布:风油精提神 知乎 编辑:程序博客网 时间:2024/06/01 22:09
public static void init(){
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String url = "jdbc:mysql://10.0.31.207:3309/gv_local?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true";
        String userName ="root";
        String pwd ="root123";
        Connection con = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url,userName,pwd);
            statement = con.prepareStatement("select * from gv_alarm_view where id in(?)");
            statement.setObject(1,new String[]{"7803","7805"});
            rs = statement.executeQuery();
            if (null != rs) {
                int i=0;
                while(rs.next()) {
                    i++;
                    System.out.println(""+rs.getLong("id"));
                }
                System.out.println("total:"+i);
            }
            
        } catch (SQLException e) {
            
            e.printStackTrace();
        }finally{
            
            close(con,statement,rs);
        }
    }
    private static void close(Connection con,Statement sta,ResultSet rs){
        
        if (null != rs) {
            
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null != sta) {
            
            try {
                sta.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (null != con) {
            
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
0 0