爪哇国新游记之十四----初试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(); } } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。