java-JDBC从数据库中读取数据并进行日期民族男女的转换

代码如下:

package com.itnba.maya.mysql;


import java.sql.*;

import java.text.SimpleDateFormat;


public class Test {
    public static void main(String[] args) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?character=GBK", "root", "");
        
        Statement state = conn.createStatement();
        String sql = "select * from info"; //查询
        ResultSet rs = state.executeQuery(sql);
        
        while(rs.next()){
            System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+(rs.getBoolean(3)?"男":"女")+"\t"+MinZu(rs.getString(4))+"\t"+Riqi(rs.getDate(5))+"\n");
        }
        
        
        
        conn.close();
        
    }
    public static String Riqi (Date data){
        SimpleDateFormat sim = new SimpleDateFormat("yyyy年MM月dd日");
        return sim.format(data);
        
    }
    
    public static String MinZu (String mz) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
        
        
        
        Statement state = conn.createStatement();
        
        String sql = "select info.code,nation.Name  from nation join info on nation.Code='"+mz+"'";
        
        ResultSet rs = state.executeQuery(sql);
        while(rs.next()){
            mz = rs.getString(2);
        }
        conn.close();
        return mz ;
        
    }
    

}

 

posted @ 2017-01-04 20:57  易小川  阅读(1727)  评论(0编辑  收藏  举报