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. }  
  • 相关阅读:
    浏览器同源政策及其规避方法---转阮大神
    js跨域详解
    js中top、self、parent
    杂记
    DOM 踩踩踩
    java idea 连接数据库
    数据库mySQL常用命令
    用迭代实现80人围成一圈逢3取出
    如何把通过类建立的对象存入数组中.
    面向对象编程
  • 原文地址:https://www.cnblogs.com/Yxxxxx/p/6853703.html
Copyright © 2011-2022 走看看