java和資料庫怎麼連接
需要oralce資料庫
1,以oralce資料庫為例
2,下載驅動
3,載入驅動
4,建立資料庫連接
注意:資料庫的驅動需要自行下載
代碼解釋:
①,驅動程序類的名稱
②,使用thin模式登陸資料庫
③,內置的一個庫
④,默認的登陸密碼
⑤,返回我們的連接
『貳』 java榪炴帴access鏁版嵁搴撶殑涓夌嶆柟寮忎互鍙婅繙紼嬭繛鎺ュ疄鐜頒唬鐮佸強鏁欑▼
榪炴帴access鏁版嵁搴擄紝緗戜笂鐨勫唴瀹瑰緢澶氾紝灝濊瘯鐨勮繃紼嬩腑涔熶細閬囧埌鍚勭嶉棶棰橈紝娌℃湁鐗瑰埆濂界殑浠嬬粛錛屾墍浠ヨ嚜宸辨兂鎬葷粨涓涓嬶紝鏃ュ悗澶囩敤銆
榪欓噷闇瑕佹彁鍓嶈存槑涓嬶紝win7緋葷粺32bit鍜64bit榛樿ゅ畨瑁呯殑access閮芥槸32bit鐨勶紝浣嗘槸濡傛灉鏄64bit鐨勭郴緇熻繛鎺access鏁版嵁搴撴椂闇瑕佸畨瑁64bit鐨刼ffice杞浠訛紝涓嬮潰浼氳村埌銆
浠64bit鎿嶄綔緋葷粺涓哄垪
絎涓縐嶏細鐢佃剳涓婇厤緗緇撳悎java浠g爜瀹炵幇
a.鎵撳紑鐩褰旵:WindowsSystem32鎵懼埌odbcad32.exe鏂囦歡錛屽弻鍑繪墦寮
b.鐐瑰嚮鍙充晶娣誨姞鎸夐挳錛屽湪鍒楄〃涓閫夋嫨access鏁版嵁搴撻┍鍔
榪欎釜鍦版柟瑕佹敞鎰忥紝浣跨敤綰痡ava瀹炵幇榪炴帴access鏁版嵁搴撶殑鏃跺欓渶瑕佸啓access鐨勯┍鍔錛岃宎ccess鐨勯┍鍔ㄥ悕縐版槸鏍規嵁榪欓噷鍐欑殑錛屼篃灝辨槸浣犳湰鏈烘湁鐨勯┍鍔錛岃屼笉鏄緗戜笂璇寸殑浠涔堣佹湁絀烘牸錛岃佹妸*.mdb, *.accdb鎹㈡垚*.mdb鎴栬*.accdb涔嬬被銆傚洜姝よ繖閲岄渶瑕佹敞鎰忋傚彟澶栬繕鍙鑳芥湁涓闂棰樸傛湁鍙鑳界偣鍑繪坊鍔犵殑鏃跺欒繖閲屽苟娌℃湁access椹卞姩錛屾垨鑰呮姤閿橻Microsoft][ODBC 椹卞姩紼嬪簭綆$悊鍣╙ 鏈鍙戠幇鏁版嵁婧愬悕縐板苟涓旀湭鎸囧畾榛樿ら┍鍔ㄧ▼搴忔椂錛
榪欎釜鏃跺欑綉涓婁笅杞戒竴涓獮ccessDatabaseEngine_X64.exe錛屽傛灉鏄32bit鎿嶄綔緋葷粺鐩稿簲鐨勪笅杞紸ccessDatabaseEngine_X32.exe鍗沖彲,鐐瑰嚮涓嬭澆64bit椹卞姩
c.鐐瑰嚮瀹屾垚鍑虹幇涓嬪垪瀵硅瘽妗嗭紝鏁版嵁婧愬悕縐伴殢鎰忓~鍐欙紝璇存槑鍙鏈夊彲鏃狅紝灝辨槸瀵規暟鎹婧愮殑鎻忚堪錛岀偣鍑婚夋嫨鎸夐挳閫夋嫨鏁版嵁搴撴墍鍦ㄧ殑璺寰
d.鐐瑰嚮紜瀹氭寜閽鍚庡氨鍙浠ュ湪ODBC鏁版嵁婧愮$悊鍣ㄤ腑鐪嬪埌娣誨姞鐨勬暟鎹婧
e.涓嬮潰鏄痡ava鐨勪唬鐮
package com.xu.bean;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectAccessDatabase {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:databaseName";//databaseName灝辨槸鍒氬垰娣誨姞鐨勬暟鎹婧愬悕縐
Connection con = DriverManager.getConnection(url, "", "");//娌℃湁鐢ㄦ埛鍚嶅拰瀵嗙爜鐨勬椂鍊欑洿鎺ヤ負絀
Statement sta = con.createStatement();
ResultSet rst = sta.executeQuery("select * from demoTable");//demoTable涓篴ccess鏁版嵁搴撲腑鐨勪竴涓琛ㄥ悕
if(rst.next()){
System.out.println(rst.getString("name"));
//瑙e喅涔辯爜闂棰
// System.out.println(new String(rst.getBytes("name"), "gbk"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} /*catch (UnsupportedEncodingException e) {
e.printStackTrace();
}*/
}
}
鐒跺悗榪愯屼唬鐮佸嵆鍙
絎浜岀嶆柟寮忥細綰痡iava瀹炵幇
a.榪欓噷闇瑕佹敞鎰忕殑灝辨槸url閭i噷錛屽墠闈涔熸彁鍒拌繃鏄鏍規嵁鐢佃剳鏈鍦版湁鐨勯┍鍔ㄥ啓鐨
package com.xu.bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 綰痡ava浠g爜瀹炵幇 榪炴帴access鏁版嵁搴
* @author xuyl
*
*/
public class ConnectAccessDatabase2 {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\demo.mdb";
Connection con = DriverManager.getConnection(url, "", "");//娌℃湁鐢ㄦ埛鍚嶅拰瀵嗙爜鐨勬椂鍊欑洿鎺ヤ負絀
Statement sta = con.createStatement();
ResultSet rst = sta.executeQuery("select * from demoTable");//demoTable涓篴ccess鏁版嵁搴撲腑鐨勪竴涓琛ㄥ悕
if(rst.next()){
System.out.println("綰痡ava浠g爜瀹炵幇:" + rst.getString("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
b.榪愯岀粨鏋
絎涓夌嶆柟寮忥細浣跨敤RmiJdbc.jar錛堜篃鏄榪滅▼榪炴帴鐨勬柟娉曪級浠ヨ繙紼嬭繛鎺ヤ負渚
棣栧厛涓嬭澆RmiJdbc.jar
鍦ㄦ斁緗瓵ccess鏁版嵁搴撶殑鐢佃剳涓婇厤緗
1.閰嶇疆RmiJDbc.jar鐜澧冨彉閲忥紙鍦ㄨ繖涔嬪墠榪橀渶瑕侀厤緗甹dk鐜澧冿紝jdk閰嶇疆鍙鑷琛岀櫨搴︼級
瑙e帇緙㏑miJdbc3.3.zip錛屽皢瑙e帇鍑虹殑鏂囦歡鏀懼湪浠繪剰鐩樼︺傚彸閿璁$畻鏈洪夋嫨灞炴 - 閫夋嫨楂樼駭緋葷粺璁劇疆 - 閫夋嫨鐜澧冨彉閲 - 鍦ㄧ郴緇熷彉閲忎腑鎵懼埌CLASSPATH閫変腑鐐瑰嚮緙栬緫鎸夐挳鍦ㄦ渶鍚庨潰娣誨姞%CLASSPATH%;D:RmiJdbc3.3RmiJdbcdistlibRmiJdbc.jar錛堜互鏀懼湪d鐩樿窡鐩褰曚負渚嬶級
2.鍚鍔≧miJbdc.jar鏈嶅姟
win + r 寤烘墦寮榪愯岀獥鍙h緭鍏cmd鎵撳紑鍛戒護紿楀彛杈撳叆
java -jar D:RmiJdbc3.3RmiJdbcdistlibRmiJdbc.jar鍑虹幇涓嬪浘鎵紺哄嵆閰嶇疆鎴愬姛銆
鍚鍔ㄤ箣鍚庨渶瑕佷竴鐩磋繍琛屻
3.閰嶇疆鏁版嵁婧
鏍規嵁涓婇潰鐨勬柟娉曢厤緗濂芥暟鎹婧
4.java浠g爜
public static String executeQuery(String sql) {
log.info("===========executeQuery start=============");
String jsonArray = "{";
StringBuilder jsonObject = new StringBuilder();
Connection con = null;
Statement stmt = null;
ResultSet rst = null;
try{
//娉ㄥ唽鏁版嵁鎻愪緵紼嬪簭
Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
//accessDatabase涓鴻繙紼嬫湇鍔″櫒鏁版嵁婧愬悕縐
// String url = "jdbc:rmi://10.23.16.83/jdbc:odbc:accessDatabase";
//鐢熶駭鍦板潃
String url = "jdbc:rmi://10.23.10.6/jdbc:odbc:accessDatabase";
// String url = "jdbc:rmi://10.23.71.211/jdbc:odbc:demo";
//寤虹珛榪炴帴
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
rst = stmt.executeQuery(sql);
if(rst != null){
while(rst.next()){
String id = rst.getInt("id") + "";
String name = new String(rst.getBytes("title"), "gbk");
String time = rst.getDate("adate").toString();
String filePath = rst.getString("url");
jsonObject.append("{"ID":"" + id + "", "NAME":"" + name + "", "FILE_PATH":"" + filePath + "", "TIME":"" + time + ""}, ");
}
String temp = jsonObject.toString();
temp = temp.substring(0, temp.length() - 2);
jsonArray += ""IS_EMPTY":"N",LIST:[" + temp + "]}";
}else{
jsonArray += ""IS_EMPTY":"Y"}";
}
}catch(Exception e){
log.info("===========executeQuery start=============" + e.getMessage());
}finally{
try {
con.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
log.info("===========executeQuery end=============");
return jsonArray;
}
浠ヤ笂灝辨槸涓夌嶈塊棶access鏁版嵁搴撶殑鏂規硶
『叄』 java怎麼與資料庫連接
使用java連接MySQL資料庫與其他的資料庫連接核心是一樣的,如果說區別,那就是所需的驅動不一樣。
工具/原料
MySQL、JDK
方法/步驟
1、首先需要安裝好JDK(配置環境變數),如圖所示:
3、最後通過代碼進行連接。
(1)確定連接路徑URL:
String url="jdbc:mysql://localhost(可以是本機IP地址):3306(埠號)/mysqltest(資料庫名稱)?"+"user=用戶賬號&password=用戶密碼&useUnicode=字元編碼";
(2)載入驅動:
Class.forName("com.mysql.jdbc.Driver");
(3)連接,獲取Connection對象
Connection conn=DriverManager.getConnection(url)
(4)可以通過conn對象檢驗連接與否。