Java串連MySQL資料庫及操作

來源:互聯網
上載者:User

標籤:

Java操作MySQL資料庫,需要驅動 mysql-connector-java 來進行操作,去下載對應的jar包 一、匯入需要的jar包我用的是maven對包進行管理,在maven中添加如下內容,直接重新匯入Reimport一下包就下載下來了<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.37</version></dependency>想要其他的版本,可以自己去maven的http://www.mvnrepository.com/去找,搜尋對應的依賴。 二、開始具體編碼1.載入驅動程式Class.forName("com.mysql.jdbc.Driver" );說明:將這個驅動載入到JVC中 2.串連資料庫(con是自訂的)Connection con = DriverManager.getConnection(url, user, password) 3.建立Statement對象,用來執行sql語句Statement stmt = con.createStatement(); 4.執行sql語句可以進行查詢、update等操作,我這裡只用到了查詢,更新可以用 executeUpdate 查詢語句:res = stmt.executeQuery(sql)說明:stmt為自己建立的Statement對象,sql為要執行的sql具體語句,是String類型的 5.提取查詢結果中的某一項while(res.next()) {long info_id = res.getLong("info_id");}說明:1.因為查詢結果可能不止一條,所以要進行遍曆,只要有這一條就可以獲得出來2.上面是查詢出了欄位名為“info_id”的項存到info_id的變數中,是long的變數類型(這裡一定要看錶設計中的欄位類型,有的是可以用int,但是有的是long)3.如果是char等類型的可以用res.getString("XXX")獲得對應的值;再有其他類型可以具體自己去查詢對應方法    我寫的一個java操作MySQL的具體代碼如下:package entity;
import java.sql.*;
import java.sql.Connection;import java.sql.Statement;  /**
* Created by lenovo on 2015/12/7.
*/
public class JDBC{
Statement stmt =null;
ResultSet rs =null;
String result = null;
public JDBC(){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://10.5.17.66:58885/?useUnicode=true&characterEncoding=utf-8";
String user = "root";
String password = "123456";
try {
Class.forName(driver);
// System.out.println("找到驅動了");
try {
Connection con = DriverManager.getConnection(url, user, password);
// System.out.println("資料庫連接成功!");
this.stmt = con.createStatement();
} catch (SQLException e) {
System.out.println("資料庫連接失敗!");
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("找不到驅動程式類 ,載入驅動失敗!");
e.printStackTrace();
}
}
//該方法返回的是全部的sql結果,在調用時需要自己去遍曆然後取得相應結果
public ResultSet getresult(String sql) {
try {this.rs = this.stmt.executeQuery(sql);return this.rs;
}catch (SQLException e){
System.out.println("SQL中找不到要尋找的欄位!");
e.printStackTrace();
return this.rs;
}
}
//該方法是直接獲得了要找的欄位名對應的結果,只取一條,所以會不斷的覆蓋,取到最後一條,試用與sql結果只有一個的情況,如果是結果是好多行資料,需要用ExcuteSql的方法
public String getresult(String sql,String key){
try {
this.rs = this.stmt.executeQuery(sql);
}catch (SQLException e){
System.out.println("SQL中找不到要尋找的欄位!");
e.printStackTrace();
}
try{
while(this.rs.next()) {
this.result = this.rs.getString(key);
}
}catch (Exception e){
System.out.println("SQL擷取結果異常!");
e.printStackTrace();
}
return this.result;
}
public static void main(String args[]) {
JDBC test = new JDBC();
String sql = "SELECT * from dbwww58com_info.info limit 1";
//會有多列結果時,只傳遞sql
ResultSet res = test.getresult(sql);
System.out.print(res);
try{
while(res.next()) {
long info_id = res.getLong("info_id");
System.out.println("\ninfo_id:" + info_id);
}
}catch (Exception e){
System.out.println("SQL擷取結果異常!");
e.printStackTrace();
}
//只有一個結果時,傳遞sql和key兩個參數
// String result= test.getresult(sql, "info_id");
// System.out.println("結果是"+result);
}
}執行結果如下: 說明:這裡寫方法getresult()時用到了重載,因為java不支援對參數賦值預設值,所以只能用重載實現這樣的功能(同一個執行方法,我一個指向獲得查詢結果,一個想獲得查詢結果中具體的某一個欄位值),關於java重載的使用,我過幾天會再寫一篇隨筆來補充一下~ 

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.