java 訪問mysql 執行個體,javamysql

來源:互聯網
上載者:User

java 訪問mysql 執行個體,javamysql

前提條件:

  1.安裝eclipse,mysql.java jdk

  2.安裝mysql connect J  (我安裝的版本是mysql connect J 5.1.39)

  3.配置java環境變數

  4.使用mysql 建立schema,然後建立表,插入資料。(我建立的表:table1,欄位:id,name)

使用eclipse編寫代碼:

  1. 建立包

  2. 建立類(勾選main選項,以便程式能執行)

  3. 完整的代碼如下:

package com.ibm.reskill.java.jdbc_sample;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcSample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Connection conn=null;
        
        //connect to the database
        try
        {
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/new_schema?useSSL=false","root","wen_201611!");
                        
            if (conn!=null) System.out.println("connect success");
            else
                System.out.println("connect failed");
            
        } catch (SQLException ex)
        {
            System.out.println("SQLException: xxx "+ex.getMessage());
            System.out.println("SQLState: "+ex.getSQLState());
            System.out.println("VendorError: "+ex.getErrorCode());
        }
        
        Statement stmt=null;
        ResultSet rs=null;
        
        try {
            stmt=conn.createStatement();
            rs=stmt.executeQuery("SELECT * FROM table1");
            
            while (rs.next())
            {
                System.out.println("The value of row "+rs.getRow()+" col 1 is "+rs.getInt(1));
                System.out.println("The value of row "+rs.getRow()+" col 2 is "+rs.getString(2));
            }
            
        } catch (SQLException ex)
        {
            System.out.println("SQLException: "+ex.getMessage());
            System.out.println("SQLState: "+ex.getSQLState());
            System.out.println("VendorError: "+ex.getErrorCode());
        }
        finally
        {
            if (rs!=null)
            {
                try{
                    rs.close();
                    System.out.println("\nrs destoryed");
                } catch (SQLException sqlEx) {}
                rs=null;
            }
            
            if (stmt!=null)
            {
                try{
                    stmt.close();
                    System.out.println("stmt destoryed");
                } catch (SQLException sqlEx){}
                stmt=null;
            }
        }
    }
}

注意事項:

如果出現找不到類的異常,嘗試修改當前項目的java build path屬性,把jdbc驅動檔案載入進來。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.