代码如下:
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 ; } }