Java串連MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

來源:互聯網
上載者:User

標籤:通過   右擊   項目匯入   over   configure   font   技術   jsp   image   

1. 資料庫1.1 建立表

在當前資料庫students中,建立資料表student:

1 mysql> create table student(2     -> studentid char(10),#學生ID3     -> name varchar(10),#學生姓名4     -> age smallint,#學生年齡5     -> grade smallint)#成績6     -> ;
1.2 插入資訊:
INSERT student VALUES("1*********","tom",18,68)

插入後是這樣:

 2. 下載connector並匯入2.1 下載解壓

在 https://dev.mysql.com/downloads/connector/j/ 下載mysql的jdbc磁碟機

解壓後:

該.jar檔案為我們需要的檔案。

2.2 建立項目匯入jar包

eclipse IDE建立Java項目,右擊Java項目:

選擇configure Build path

兩種方式添加jar檔案都能實現。

其中區別在於:

add jars需要事先將jar檔案複製到該項目裡面來,點擊add jars按鈕之後,在該項目裡選擇該jar檔案。使用的是相對路徑,即使把該項目複製到別的電腦也可以使用。

add external Jars 則是用的絕對路徑,在資源管理員裡面選擇jar檔案。並且之後會出現:

3. 代碼3.1 編寫代碼:
import java.sql.*;import com.mysql.*;public class Test1 {    public static void main(String[] args) throws Exception {        // TODO Auto-generated method stub        Class.forName("com.mysql.jdbc.Driver");        java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?useSSL=true","root","11111");        java.sql.Statement sql=conn.createStatement();        java.sql.ResultSet rs=sql.executeQuery("select * from student");                while(rs.next())        {            System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4));        }    }}
 3.2 代碼講解:
  • Class.forName() 是用來載入驅動。Class.forName()的用法詳細介紹:http://blog.csdn.net/kaiwii/article/details/7405761
  • getConnection()的用法:

       

參數url, jdbc:mysql://localhost:3306/students

注意連接埠號碼3306是預設的連接埠號碼,如果更改了,此處也要更改。斜杠右邊是資料庫名稱。

之所以代碼裡面後面加? useSSL=true,是因為如果不加,會出現如下警告:

 Warning: Establishing SSL connection without server‘s identity verification is not recommended 

  回答見stackoverflow: http://stackoverflow.com/questions/34224970/warning-establishing-ssl-connection-without-servers-identity-verification-is-n

user就是使用者名稱,password密碼。

 也可以這樣:

jdbc:mysql://localhost:3306/jspstudy?user=root&password=111111

  

 查詢的結果在rs對象裡面,通過裡面的getString()方法來得到字串輸出,參數裡面的整數是列數。

代碼和查詢結果:

 

 

Java串連MySQL Warning: Establishing SSL connection without server's identity verification is not recommended

聯繫我們

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