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

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

时间:2017-01-04 21:52:18      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:读取数据   nat   except   nbsp   resultset   pre   for   maya   creat   

代码如下:

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从数据库中读取数据并进行日期民族男女的转换

标签:读取数据   nat   except   nbsp   resultset   pre   for   maya   creat   

原文地址:http://www.cnblogs.com/kuangwong/p/6250205.html

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