Java連接MySql的詳細(xì)介紹
更新時(shí)間:2013年04月25日 14:37:17 作者:
本篇文章主要是對(duì)Java連接MySql的詳細(xì)介紹。需要的朋友參考下
1.
現(xiàn)在工程(不是Src)上右鍵--Build Path--Add External Archives,選擇驅(qū)動(dòng)下的那個(gè)jar包,這是release版本,bin目錄下的是debug版本。
示例在docs下的connector-j.html,里面有例子(其中的test是數(shù)據(jù)庫(kù)名,換位自己的)。
復(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控制臺(tái)下創(chuàng)建數(shù)據(jù)庫(kù),也可以在通過(guò)執(zhí)行 "\. 絕對(duì)路徑名"。
“--”是注釋符。
復(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ù)庫(kù)亂碼的解決方法
- Java連接MYSQL數(shù)據(jù)庫(kù)的實(shí)現(xiàn)步驟
- java連接mysql數(shù)據(jù)庫(kù)詳細(xì)步驟解析
- java連接MySQl數(shù)據(jù)庫(kù)實(shí)例代碼
- java連接Mysql數(shù)據(jù)庫(kù)的工具類(lèi)
- java連接MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)代碼
- JavaWeb連接數(shù)據(jù)庫(kù)MySQL的操作技巧
- javaweb中mysql數(shù)據(jù)庫(kù)連接步驟方法及其實(shí)例
- java連接mysql數(shù)據(jù)庫(kù)的方法
- Java+MySQL前后端連接新手小白教程
相關(guān)文章
MySQL數(shù)據(jù)庫(kù)高可用HA實(shí)現(xiàn)小結(jié)
MySQL數(shù)據(jù)庫(kù)是目前開(kāi)源應(yīng)用最大的關(guān)系型數(shù)據(jù)庫(kù),有海量的應(yīng)用將數(shù)據(jù)存儲(chǔ)在MySQL數(shù)據(jù)庫(kù)中,這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)高可用HA實(shí)現(xiàn),需要的朋友可以參考下2022-01-01
MySQL基于SSL安全連接的主從復(fù)制(過(guò)程詳解)
SSL(Secure Sockets Layer 安全套接層),及其繼任者傳輸層安全(Transport Layer Security,TLS)是為網(wǎng)絡(luò)通信提供安全及數(shù)據(jù)完整性的一種安全協(xié)議,這篇文章主要介紹了MySQL基于SSL安全連接的主從復(fù)制,需要的朋友可以參考下2023-04-04
mysql實(shí)現(xiàn)將字符串轉(zhuǎn)化成int類(lèi)型
這篇文章主要介紹了mysql實(shí)現(xiàn)將字符串轉(zhuǎn)化成int類(lèi)型方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
MySQL中slave_exec_mode參數(shù)詳解
本篇文章主要給大家講述了MySQL中slave_exec_mode參數(shù)的用法以及示例分析了出現(xiàn)的錯(cuò)誤問(wèn)題和解決辦法,需要的朋友參考學(xué)習(xí)下吧。2017-12-12

