java 串連mysql資料庫並建立表

來源:互聯網
上載者:User

標籤:nec   summary   https   tor   文檔   color   charset   imp   arch   

java連結mysql資料庫需要驅動包,那麼我們先去下載驅動包http://dev.mysql.com/downloads/connector/j/,驅動包就是一個jar包裡麵包含了我們要使用的類、方法等

。將下載的驅動包匯入我們的項目,就可以寫程式建立我們的表了。具體可以查閱API文檔https://docs.oracle.com/javase/8/docs/api/overview-summary.html

 1 import java.sql.*; 2 public class Linksql { 3  4     //我麼要執行建立表的DDl語句 5     static String creatsql = "CREATE TABLE pepole(" 6             + "name varchar(10) not null," 7             + "age int(4) not null" 8             + ")charset=utf8;"; 9 10     final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";11         //指定串連資料庫的url12     final static String DB_URL = "jdbc:mysql://localhost/student";13     //mysql使用者名稱14     final static String name = "root";15         //mysql密碼16     final static String pwd = "pwd";17     public static void main(String[] args)18     {19       Connection conn = null;20       Statement stmt = null;21       try22       {23           //註冊jdbc驅動24           Class.forName(JDBC_DRIVER);25           //開啟串連26           System.out.println("//串連資料庫");27           conn = DriverManager.getConnection(DB_URL,name,pwd);28           //執行建立表29           System.out.println("//建立表");30           stmt = conn.createStatement();31          if(0 == stmt.executeLargeUpdate(creatsql))32          {33              System.out.println("成功建立表!");34          }35          else36          {37              System.out.println("建立表失敗!");38          }39          //40           stmt.close();41           conn.close();42           System.out.println("//關閉資源");43       }44       catch(Exception e)45       {46           System.out.println("建立表失敗!");47           e.printStackTrace();48       }49     }50 }51     

 

java 串連mysql資料庫並建立表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.