码迷,mamicode.com
首页 > 数据库 > 详细

JDBC基本操作示例

时间:2017-05-14 21:50:02      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:bar   use   sel   idt   otf   exception   copyto   port   rest   

 

[java] view plain copy
 
 技术分享技术分享
    1. import java.sql.Connection;  
    2. import java.sql.DriverManager;  
    3. import java.sql.PreparedStatement;  
    4. import java.sql.ResultSet;  
    5. import java.sql.SQLException;  
    6.   
    7. public class TestJDBC {  
    8.   
    9.     public static void main(String[] args) {  
    10.   
    11.         Connection conn = null;  
    12.         PreparedStatement pstmt = null;  
    13.         ResultSet rs = null;  
    14.   
    15.         try {  
    16.             Class.forName("com.mysql.jdbc.Driver");  
    17.   
    18.             conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "root");  
    19.   
    20.             pstmt = conn.prepareStatement("select * from _user");  
    21.   
    22.             rs = pstmt.executeQuery();  
    23.   
    24.             while (rs.next()) {  
    25.                 System.out.println(rs.getString("username"));  
    26.             }  
    27.         } catch (ClassNotFoundException e) {  
    28.             e.printStackTrace();  
    29.         } catch (SQLException e) {  
    30.             e.printStackTrace();  
    31.         } finally {  
    32.             if (rs != null) {  
    33.                 try {  
    34.                     rs.close();  
    35.                 } catch (SQLException e) {  
    36.                     e.printStackTrace();  
    37.                 }  
    38.             }  
    39.   
    40.             if (pstmt != null) {  
    41.                 try {  
    42.                     rs.close();  
    43.                 } catch (SQLException e) {  
    44.                     e.printStackTrace();  
    45.                 }  
    46.             }  
    47.   
    48.             if (conn != null) {  
    49.                 try {  
    50.                     rs.close();  
    51.                 } catch (SQLException e) {  
    52.                     e.printStackTrace();  
    53.                 }  
    54.             }  
    55.         }  
    56.     }  
    57. }  

JDBC基本操作示例

标签:bar   use   sel   idt   otf   exception   copyto   port   rest   

原文地址:http://www.cnblogs.com/Yxxxxx/p/6853703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!