日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

Java連接MySql的詳細(xì)介紹_MySQL教程

編輯Tag賺U幣
教程Tag:MySQLJava添加

推薦:基于unique與primary約束的區(qū)別分析
本篇文章介紹了unique與primary約束的區(qū)別分析。需要的朋友參考下

 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ù)制代碼 代碼如下:www.hl5o.cn

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. 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ù)制代碼 代碼如下:www.hl5o.cn

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();
}
}

}

}

分享:網(wǎng)站數(shù)據(jù)多了分頁(yè)慢該怎么辦?
網(wǎng)站數(shù)據(jù)多了分頁(yè)慢該怎么辦?在使用 MySQL 數(shù)據(jù)庫(kù)時(shí)大偏移量的數(shù)據(jù)查詢(xún)是非常慢的,如何通過(guò)優(yōu)化SQL語(yǔ)句來(lái)加速分頁(yè)查詢(xún)呢? 工具/原料MySQL 數(shù)據(jù)庫(kù)Apache (WEB服務(wù)器軟件)方法/步驟分析傳統(tǒng)分頁(yè)SQL語(yǔ)句 select * from table limit $offset, 10,當(dāng)$offset非常大時(shí),例如

來(lái)源:模板無(wú)憂//所屬分類(lèi):MySQL教程/更新時(shí)間:2013-04-26
相關(guān)MySQL教程