JDBC建立表實例

在本教程將演示如何在JDBC應用程序中建立一個數據庫表。 在執行如下示例以前,請確保您已經準備好如下操做:java

  • 具備數據庫管理員權限,以在給定模式中建立數據庫表。 要執行如下示例,須要用實際用戶名和密碼替換這裏用戶名(username)和密碼(password)。
  • MySQL或數據庫已啓動並運行。

所需步驟

使用JDBC應用程序建立新數據庫須要如下步驟:mysql

  • 導入包:須要包含包含數據庫編程所需的JDBC類的包。 大多數狀況下,使用import java.sql.*就足夠了。sql

  • 註冊JDBC驅動程序:須要初始化驅動程序,以即可以程序中打開數據庫的通訊通道。shell

  • 打開鏈接:須要使用DriverManager.getConnection()方法來建立一個Connection對象,它表示與數據庫服務器的物理鏈接。數據庫

  • 執行查詢:須要使用類型爲Statement的對象來構建和提交SQL語句,以在選擇的數據庫中建立一個表。編程

  • 清理環境:須要明確地關閉全部數據庫資源,而不依賴於JVM的垃圾收集。服務器

示例代碼

複製如下示例代碼保存到文件:CreateTable.java中,而後編譯並運行以下 -app

//STEP 1. Import required packages // See more detail at http://www.yiibai.com/jdbc/ import java.sql.*; public class CreateTable { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/jdbc_db"; // Database credentials static final String USER = "root"; static final String PASS = "123456"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to a selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Connected database successfully..."); //STEP 4: Execute a query System.out.println("Creating table in given database..."); stmt = conn.createStatement(); String sql = "CREATE TABLE student " + "(id INTEGER not NULL, " + " first VARCHAR(255), " + " last VARCHAR(255), " + " age INTEGER, " + " PRIMARY KEY ( id ))"; stmt.executeUpdate(sql); System.out.println("Created table in given database..."); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(stmt!=null) conn.close(); }catch(SQLException se){ }// do nothing try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main }//end JDBCExample 
Java

編譯上面代碼,以下 -yii

F:\worksp\jdbc> javac -Djava.ext.dirs=F:\worksp\jdbc\libs CreateTable.java
Shell

執行上面代碼,以下 -ide

##F:\worksp\jdbc>java -Djava.ext.dirs=F:\worksp\jdbc\libs CreateDatabase

F:\worksp\jdbc>java -Djava.ext.dirs=F:\worksp\jdbc\libs CreateTable
Connecting to a selected database...
Thu Jun 01 22:17:34 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Connected database successfully...
Creating table in given database...
Created table in given database...
Goodbye!

F:\worksp\jdbc>
Shell
相關文章
相關標籤/搜索