zoukankan      html  css  js  c++  java
  • JDBC基本操作示例

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      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. }  
  • 相关阅读:
    Eclipse常用开发插件
    getOutputStream() 的问题
    JSP内置对象之WEB安全性及config对象
    windows开机后键盘失灵(非硬件原因)解决办法
    Eclipse下如何导入jar包
    更改Eclipse下Tomcat的部署目录 ,防止上传的文件是到eclipse的克隆的tomcat上的webapp,而不是tomcat本身的webapp
    大数据的挑战:数据质量和历史偏见
    HR数据分析常用的50个公式
    HR数据分析常用的50个公式
    python中的随机函数random的用法示例
  • 原文地址:https://www.cnblogs.com/Yxxxxx/p/6853703.html
Copyright © 2011-2022 走看看