日期:2014-05-20 浏览次数:20897 次
package net.icefish.bean; import java.sql.*; public class Conn{ //public Connection conn=null; public static Connection Connection(){ //public conn(){ Connection conn=null; try{ // request.setCharacterEncoding("gb2312"); Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch(Exception e){ System.out.print(e.toString()); } try{ String url="jdbc:mysql://127.0.0.1:3306/icefish?useUnicode=true&characterEncoding=gb2312"; //数据库名统一为icefish,表名与字段名参照概要设计文档 String user="root"; //设置用户名 String password="root"; //设置密码 conn=DriverManager.getConnection(url,user,password); } catch(SQLException e){ System.out.print(e.toString()); } return conn; } // public void close(){ // try{ // conn.close(); // } // catch(SQLException e){ // System.out.print(e.toString()); // } // } }
package net.icefish.bean; import java.sql.*; import net.icefish.bean.Conn; public class test{ public static void main(String[] args){ try{ Connection icefish=Conn.connection(); Statement stmt=icefish.createStatement(); ResultSet rs=stmt.executeQuery("select * from icefish_admin"); while(rs.next()) System.out.print(rs.getString(1)+" "); icefish.close(); }catch(SQLException e){ System.out.print(e.toString()); } } }