Java連接MySql的詳細(xì)介紹
更新時(shí)間:2013年04月25日 14:37:17 作者:
本篇文章主要是對Java連接MySql的詳細(xì)介紹。需要的朋友參考下
1.
現(xiàn)在工程(不是Src)上右鍵--Build Path--Add External Archives,選擇驅(qū)動下的那個(gè)jar包,這是release版本,bin目錄下的是debug版本。
示例在docs下的connector-j.html,里面有例子(其中的test是數(shù)據(jù)庫名,換位自己的)。
復(fù)制代碼 代碼如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=monty&password=greatsqldb");
// Do something with the Connection
...
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
2.可以直接在MySql控制臺下創(chuàng)建數(shù)據(jù)庫,也可以在通過執(zhí)行 "\. 絕對路徑名"。
“--”是注釋符。
復(fù)制代碼 代碼如下:
View Code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class mysql {
/**
* @param args
*/
public static void main(String[] args) {// 多個(gè)try合并到一塊,然后使用source --- format
// TODO Auto-generated method stub
//若是用到finally則需要把聲明放在try外邊
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");// 后面若是加上".newInstance"則還需要加上幾個(gè)拋出異常
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"
+ "user=root&password=root");
/*
* java.sql.Statement; 不是com.mysql這個(gè)包; 二者不可以同時(shí)存在
*/
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from info");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
// Do something with the Connection
} catch (ClassNotFoundException ex) {
// handle any errors
ex.printStackTrace();
} catch (SQLException ex) {
// TODO Auto-generated catch block
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if(null!= rs) {
rs.close();
rs = null;
}
if(null!= stmt) {
stmt.close();
stmt = null;
}
if(null!= conn) {
conn.close();
conn = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
您可能感興趣的文章:
- java連接mysql數(shù)據(jù)庫亂碼的解決方法
- Java連接MYSQL數(shù)據(jù)庫的實(shí)現(xiàn)步驟
- java連接mysql數(shù)據(jù)庫詳細(xì)步驟解析
- java連接MySQl數(shù)據(jù)庫實(shí)例代碼
- java連接Mysql數(shù)據(jù)庫的工具類
- java連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)代碼
- JavaWeb連接數(shù)據(jù)庫MySQL的操作技巧
- javaweb中mysql數(shù)據(jù)庫連接步驟方法及其實(shí)例
- java連接mysql數(shù)據(jù)庫的方法
- Java+MySQL前后端連接新手小白教程
相關(guān)文章
Mysql數(shù)據(jù)庫之常用sql語句進(jìn)階與總結(jié)
這篇文章主要介紹了Mysql數(shù)據(jù)庫之常用sql語句,總結(jié)分析了MySQL數(shù)據(jù)庫常用的查詢、條件查詢、排序、連接查詢、子查詢等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11Ubuntu Server 16.04下mysql8.0安裝配置圖文教程
這篇文章主要為大家詳細(xì)介紹了Ubuntu Server 16.04下mysql8.0安裝配置圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05利用MySQL主從配置實(shí)現(xiàn)讀寫分離減輕數(shù)據(jù)庫壓力
今天小編就為大家分享一篇關(guān)于利用MySQL主從配置實(shí)現(xiàn)讀寫分離減輕數(shù)據(jù)庫壓力,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03