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对象检验连接与否。