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

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 ;
        
    }
    

}

 

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