JDBC小小实例教程

  1. public class Base {  
  2.   
  3.     public static void main(String[] args) throws Exception {  
  4.         // TODO Auto-generated method stub  
  5.         template();  
  6.     }  
  7.     static void template() throws Exception  
  8.     {  
  9.           
  10.         Connection conn=null;  
  11.         Statement st=null;  
  12.         ResultSet resultset=null;  
  13.           
  14.         try {  
  15.             //2.建立连接  
  16.             conn=JdbcUtils.getConnection();  
  17.             //3.创建语句  
  18.             st=conn.createStatement();  
  19.             //4.执行语句  
  20.             resultset=st.executeQuery("select *from user");  
  21.             //5.处理结果  
  22.             while(resultset.next())  
  23.             {  
  24.                 System.out.println(resultset.getObject(1));  
  25.                 System.out.println(resultset.getObject(2));  
  26.                 System.out.println(resultset.getObject(3));  
  27.                 System.out.println(resultset.getObject(4));  
  28.             }  
  29.               
  30.         } finally  
  31.         {  
  32.             JdbcUtils.free(resultset, st, conn);  
  33.         }  
  34.           
  35.     }}  
  36. public class JdbcUtils   
  37. {  
  38.     private static String url="jdbc:mysql://localhost:3306/jdbc";  
  39.     private static String user="root";  
  40.     private static String password="123";  
  41.     private JdbcUtils()  
  42.     {  
  43.     }  
  44.     static  
  45.     {  
  46.         try  
  47.         {  
  48.             Class.forName("com.mysql.jdbc.Driver");  
  49.         }  
  50.         catch(ClassNotFoundException e)  
  51.         {  
  52.             throw new ExceptionInInitializerError(e);  
  53.         }  
  54.     }  
  55.     public static Connection getConnection() throws SQLException  
  56.     {  
  57.         return DriverManager.getConnection(url, userpassword);  
  58.     }  
  59.     public static void free(ResultSet resultset,Statement st,Connection conn)  
  60.     {  
  61.         //6.释放资源  
  62.         try{  
  63.         if(resultset!=null)  
  64.             resultset.close();  
  65.         } catch (SQLException e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         }  
  69.         finally  
  70.         {  
  71.             try  
  72.             {  
  73.                 if(st!=null)  
  74.                     st.close();  
  75.             } catch (SQLException e) {  
  76.                 // TODO Auto-generated catch block  
  77.                 e.printStackTrace();  
  78.             }  
  79.             finally  
  80.             {  
  81.                 if(conn!=null)  
  82.                     try {  
  83.                         conn.close();  
  84.                     } catch (SQLException e) {  
  85.                         // TODO Auto-generated catch block  
  86.                         e.printStackTrace();  
  87.                     }  
  88.             }  
  89.         }  
  90.     }  
  91. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值