• JDBC初探(一)


    下载好JDBC之后,首先做的应该是查看它的文档

    打开connector-j.html

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.SQLException;
     4 
     5 // Notice, do not import com.mysql.jdbc.*
     6 // or you will have problems!
     7 
     8 public class LoadDriver {
     9     public static void main(String[] args) {
    10         try {
    11             // The newInstance() call is a work around for some
    12             // broken Java implementations
    13 
    14             Class.forName("com.mysql.jdbc.Driver").newInstance();
    15         } catch (Exception ex) {
    16             // handle the error
    17         }
    18     }
    19 }

    这是注册MySQL Connector/J,之后

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.SQLException;
     4 
     5 Connection conn = null;
     6 ...
     7 try {
     8     conn =
     9        DriverManager.getConnection("jdbc:mysql://localhost/test?" +
    10                                    "user=minty&password=greatsqldb");
    11 
    12     // Do something with the Connection
    13 
    14    ...
    15 } catch (SQLException ex) {
    16     // handle any errors
    17     System.out.println("SQLException: " + ex.getMessage());
    18     System.out.println("SQLState: " + ex.getSQLState());
    19     System.out.println("VendorError: " + ex.getErrorCode());
    20 }

    连接数据库,之后Using JDBC Statement Objects to Execute SQL

     1 import java.sql.Connection;
     2 import java.sql.DriverManager;
     3 import java.sql.SQLException;
     4 import java.sql.Statement;
     5 import java.sql.ResultSet;
     6 
     7 // assume that conn is an already created JDBC connection (see previous examples)
     8 
     9 Statement stmt = null;
    10 ResultSet rs = null;
    11 
    12 try {
    13     stmt = conn.createStatement();
    14     rs = stmt.executeQuery("SELECT foo FROM bar");
    15 
    16     // or alternatively, if you don't know ahead of time that
    17     // the query will be a SELECT...
    18 
    19     if (stmt.execute("SELECT foo FROM bar")) {
    20         rs = stmt.getResultSet();
    21     }
    22 
    23     // Now do something with the ResultSet ....
    24 }
    25 catch (SQLException ex){
    26     // handle any errors
    27     System.out.println("SQLException: " + ex.getMessage());
    28     System.out.println("SQLState: " + ex.getSQLState());
    29     System.out.println("VendorError: " + ex.getErrorCode());
    30 }
    31 finally {
    32     // it is a good idea to release
    33     // resources in a finally{} block
    34     // in reverse-order of their creation
    35     // if they are no-longer needed
    36 
    37     if (rs != null) {
    38         try {
    39             rs.close();
    40         } catch (SQLException sqlEx) { } // ignore
    41 
    42         rs = null;
    43     }
    44 
    45     if (stmt != null) {
    46         try {
    47             stmt.close();
    48         } catch (SQLException sqlEx) { } // ignore
    49 
    50         stmt = null;
    51     }
    52 }


    至此,就可以完成数据库的基本操作了

  • 相关阅读:
    得到相对Plugin的路径
    GEF常见问题4:非矩形图元
    在Eclipse的About对话框上添加自己的图标
    用GMF生成简化的数据库设计器
    全超实用的Javascript类库和jQuery插件大全之一:图片,地图和图形
    #敏捷个人# 第二批敏捷个人推广者实践团报名
    2012年最新的12款超棒jQuery插件
    《敏捷个人》周刊 第14期 (可下载)
    时间管理:敏捷个人时中法卡片
    #敏捷个人# 实践团报名
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5340176.html
Copyright © 2020-2023  润新知