首页 > 代码库 > 爪哇国新游记之十四----初试JDBC

爪哇国新游记之十四----初试JDBC

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class A{    public static void search1(){        Connection conn=null;        PreparedStatement ps=null;        ResultSet rs=null;                try{            Class.forName("com.mysql.jdbc.Driver");            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1/test", "root", "hy");                        String sql=" select * from employee order by salary ";            ps=conn.prepareStatement(sql);            rs=ps.executeQuery();                                    while(rs.next()){                System.out.print(rs.getString("id")+"\t");                System.out.print(rs.getString("name")+"\t");                System.out.print(rs.getString("sex")+"\t");                System.out.print(rs.getInt("salary")+"\t");                System.out.println();            }        }        catch(SQLException se){            try{                conn.rollback();            }            catch(Exception ex){                ex.printStackTrace();            }        }        catch(Exception e){            try{                conn.rollback();            }            catch(Exception ex){                ex.printStackTrace();            }        }        finally{            try{                rs.close();                ps.close();                conn.close();            }            catch(Exception ex){                ex.printStackTrace();            }        }    }}