zoukankan      html  css  js  c++  java
  • 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)+"	"+rs.getString(2)+"	"+(rs.getBoolean(3)?"男":"女")+"	"+MinZu(rs.getString(4))+"	"+Riqi(rs.getDate(5))+"
    ");
            }
            
            
            
            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 ;
            
        }
        
    
    }
  • 相关阅读:
    设计模式之工厂方法3
    Orchard CMS中如何打包不带源码的模块
    Dump Checking
    认识WinDbg
    Bootstrap3.0学习第九轮(CSS补充)
    SVN版本冲突解决详解
    windbg Symbol file path
    SVN下错误集锦
    MvcMovieStore mvc5.0,EF6.01
    SQL Server中的窗口函数
  • 原文地址:https://www.cnblogs.com/kuangwong/p/6250205.html
Copyright © 2011-2022 走看看