java和數(shù)據(jù)庫連接代碼 javaweb數(shù)據(jù)庫連接代碼

java連接數(shù)據(jù)庫的代碼

用這個(gè)類吧.好的話,給我加加分.

成都創(chuàng)新互聯(lián)公司一直在為企業(yè)提供服務(wù),多年的磨煉,使我們?cè)趧?chuàng)意設(shè)計(jì),營銷型網(wǎng)站到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗(yàn)。我們擅長傾聽企業(yè)需求,挖掘用戶對(duì)產(chǎn)品需求服務(wù)價(jià)值,為企業(yè)制作有用的創(chuàng)意設(shè)計(jì)體驗(yàn)。核心團(tuán)隊(duì)擁有超過十年以上行業(yè)經(jīng)驗(yàn),涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)綿陽服務(wù)器托管、成都app軟件開發(fā)、手機(jī)移動(dòng)建站、網(wǎng)頁設(shè)計(jì)、網(wǎng)絡(luò)整合營銷。

import java.sql.*;

/**

* @功能: 一個(gè)JDBC的本地化API連接類,封裝了數(shù)據(jù)操作方法,只用傳一個(gè)SQL語句即可

* @作者: 李開歡

* @日期: 2007/

*/

public class ConnectionDemo {

/*

* 這里可以將常量全部放入另一個(gè)類中,以方便修改

*/

private static Connection conn;

private static Statement ps;

private static ResultSet rs;

private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

private static final String USER ="sa";

private static final String PASS = "sa";

public ConnectionDemo() {

// TODO Auto-generated constructor stub

ConnectionDemo.getConnection();

}

public static Connection getConnection(){

System.out.println("連接中...");

try {

Class.forName(ConnectionDemo.DRIVER);

conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);

System.out.println("成功連接");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static Statement getStatement(String sql){

System.out.println("執(zhí)行SQL語句中...");

try {

ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

if(sql.substring(0, 6).equals("select")){

rs = ps.executeQuery(sql);

System.out.println("執(zhí)行完查詢操作,結(jié)果已返回ResultSet集合");

}else if(sql.substring(0, 6).equals("delete")){

ps.executeUpdate(sql);

System.out.println("已執(zhí)行完畢刪除操作");

}else if(sql.substring(0, 6).equals("insert")){

ps.executeUpdate(sql);

System.out.println("已執(zhí)行完畢增加操作");

}else{

ps.executeUpdate(sql);

System.out.println("已執(zhí)行完畢更新操作");

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return ps;

}

public static ResultSet getResultSet(){

System.out.println("查詢結(jié)果為:");

return rs;

}

public static void closeConnection(){

System.out.println("關(guān)閉連接中...");

try {

if (rs != null) {

rs.close();

System.out.println("已關(guān)閉ResultSet");

}

if (ps != null) {

ps.close();

System.out.println("已關(guān)閉Statement");

}

if (conn != null) {

conn.close();

System.out.println("已關(guān)閉Connection");

}

} catch (Exception e) {

// TODO: handle exception

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

ConnectionDemo.getConnection();

String sql = "delete from type where id = 1";

ConnectionDemo.getStatement(sql);

String sql2 = "insert into type values(1,'教學(xué)設(shè)備')";

ConnectionDemo.getStatement(sql2);

String sql1 = "select * from type";

ConnectionDemo.getStatement(sql1);

ResultSet rs = ConnectionDemo.getResultSet();

System.out.println("編號(hào) "+"類 型");

try {

while(rs.next()){

System.out.print(" "+rs.getInt(1)+" ");

System.out.println(rs.getString(2));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

ConnectionDemo.closeConnection();

}

}

Java中如何實(shí)現(xiàn)與后臺(tái)數(shù)據(jù)庫的連接?

用JAVA連接數(shù)據(jù)庫主要有兩種方式,一是用JDBC-ODBC橋來連接,二是用相關(guān)廠商提供的相應(yīng)驅(qū)動(dòng)程序來連接,首先談?wù)劦谝环N連接。 \x0d\x0a\x0d\x0aJDBC-ODBC橋接器是用JdbcOdbc.Class和一個(gè)用于訪問ODBC驅(qū)動(dòng)程序的本地庫實(shí)現(xiàn)的。對(duì)于WINDOWS平臺(tái),該本地庫是一個(gè)動(dòng)態(tài)連接庫DLL(JDBCODBC.DLL)。 \x0d\x0a\x0d\x0a由于JDBC在設(shè)計(jì)上與ODBC很接近。在內(nèi)部,這個(gè)驅(qū)動(dòng)程序把JDBC的方法映射到ODBC調(diào)用上,這樣,JDBC就可以和任何可用的ODBC驅(qū)動(dòng)程序進(jìn)行交互了。這種橋接器的優(yōu)點(diǎn)是,它使JDBC目前有能力訪問幾乎所有的數(shù)據(jù)庫。通行方式如圖所示: \x0d\x0a\x0d\x0a應(yīng)用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC層---數(shù)據(jù)源 \x0d\x0a\x0d\x0a具體操作方法為: \x0d\x0a\x0d\x0a首先打開控制面板的管理工具,打開數(shù)據(jù)源(ODBC),在用戶DSN里面添加數(shù)據(jù)源(即你要連接的數(shù)據(jù)庫的名字),在這里假定連接SQL SERVER 2000的GoodsSupply數(shù)據(jù)庫。名稱填寫你要連接的數(shù)據(jù)庫的名稱(GoodsSupply),然后逐步設(shè)置,如果選用了使用SQL-SERVER密碼認(rèn)證的話,就要輸入相應(yīng)的用戶名及密碼連接到數(shù)據(jù)庫。一路下一步設(shè)置完成。 \x0d\x0a\x0d\x0a在JAVA里面編寫程序進(jìn)行測(cè)試,在這里我的程序是讓用戶輸入任意的表名與與列名,把該列的所有數(shù)據(jù)輸出。源代碼如下: \x0d\x0a\x0d\x0aimport java.io.BufferedReader; \x0d\x0aimport java.io.InputStreamReader; \x0d\x0aimport java.sql.*; \x0d\x0a\x0d\x0apublic class ODBCBridge { \x0d\x0a\x0d\x0apublic static void main(String[] args) { \x0d\x0aString url="jdbc:odbc:GoodsSupply"; \x0d\x0aStatement sm=null; \x0d\x0aString command=null; \x0d\x0aResultSet rs=null; \x0d\x0aString tableName=null; \x0d\x0aString cName=null; \x0d\x0aString result=null; \x0d\x0aBufferedReader input=new BufferedReader(new InputStreamReader(System.in)); \x0d\x0atry { \x0d\x0atry { \x0d\x0aClass.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加載驅(qū)動(dòng) \x0d\x0a}catch(ClassNotFoundException e){ \x0d\x0aSystem.out.println("Can not load Jdbc-Odbc Bridge Driver"); \x0d\x0aSystem.err.print("ClassNotFoundException:"); \x0d\x0aSystem.err.println(e.getMessage()); \x0d\x0a} \x0d\x0aConnection con=DriverManager.getConnection(url,"USER","PASSWORD"); //使用SQL-SERVER2000認(rèn)證 \x0d\x0aDatabaseMetaData dmd=con.getMetaData(); //DMD為連接的相應(yīng)情況 \x0d\x0aSystem.out.println("連接的數(shù)據(jù)庫:"+dmd.getURL()); \x0d\x0aSystem.out.println("驅(qū)動(dòng)程序:"+dmd.getDriverName()); \x0d\x0asm=con.createStatement(); \x0d\x0aSystem.out.println("輸入表名"); \x0d\x0atableName=input.readLine(); \x0d\x0awhile(true) { \x0d\x0aSystem.out.println("輸入列名(為空時(shí)程序結(jié)束):"); \x0d\x0acName=input.readLine(); \x0d\x0aif(cName.equalsIgnoreCase("")) \x0d\x0abreak; \x0d\x0acommand="select "+cName+" from "+tableName; \x0d\x0ars=sm.executeQuery(command); //執(zhí)行查詢 \x0d\x0aif(!rs.next()) \x0d\x0aSystem.out.println("表名或列名輸入有誤"); \x0d\x0aelse { \x0d\x0aSystem.out.println("查詢結(jié)果為:"); \x0d\x0ado \x0d\x0a{ \x0d\x0aresult=rs.getString(cName); \x0d\x0a//數(shù)據(jù)庫語言設(shè)置為中文,不用轉(zhuǎn)換編碼 \x0d\x0a//result=new String(result.getBytes("ISO-8859-1"),"GB2312"); \x0d\x0aSystem.out.println(result); \x0d\x0a}while(rs.next()); \x0d\x0a} \x0d\x0a} \x0d\x0a}catch(SQLException ex) { \x0d\x0aSystem.out.println("SQLException:"); \x0d\x0awhile(ex!=null) { \x0d\x0aSystem.out.println("Message:"+ex.getMessage()); \x0d\x0aex=ex.getNextException(); \x0d\x0a} \x0d\x0a}catch(Exception e) { \x0d\x0aSystem.out.println("IOException"); \x0d\x0a} \x0d\x0a} \x0d\x0a}

JAVA連接數(shù)據(jù)庫連接代碼怎么寫?

//連接mysql,先導(dǎo)入mysql驅(qū)動(dòng)

Connection?conn;?//?聲明Connectoion對(duì)象

String?driver?=?"com.mysql.jdbc.Driver";?//?驅(qū)動(dòng)程序名

//oracle,先導(dǎo)入oracle驅(qū)動(dòng)

//Class.forName("oracle.jdbc.driver.OracleDriver");

//String?url="jdbc:oracle:this@localhost:1521:testdb ";????//中間冒號(hào)分隔

String?url?=?"jdbc:mysql://localhost:3306/testdb";?//?要訪問的數(shù)據(jù)庫

String?user?=?"root";

String?password?=?"root";

Class.forName(driver);?//?加載驅(qū)動(dòng)

conn?=?DriverManager.getConnection(url,?user,?password);

求java與數(shù)據(jù)庫連接的代碼(含注釋)

代碼主要列出連接數(shù)據(jù)庫的關(guān)鍵代碼,其他訪問數(shù)據(jù)庫代碼省略

1、Oracle8/8i/9i數(shù)據(jù)庫(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

String url="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl為數(shù)據(jù)庫的SID

String user="test";

String password="test";

Connection conn= DriverManager.getConnection(url,user,password);

2、DB2數(shù)據(jù)庫

Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();

String url="jdbc:db2://localhost:5000/sample";

//sample為你的數(shù)據(jù)庫名

String user="admin";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);

3、Sql Server7.0/2000數(shù)據(jù)庫

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";

//mydb為數(shù)據(jù)庫

String user="sa";

String password="";

Connection conn= DriverManager.getConnection(url,user,password);

4、Sybase數(shù)據(jù)庫

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

String url =" jdbc:sybase:Tds:localhost:5007/myDB";

//myDB為你的數(shù)據(jù)庫名

Properties sysProps = System.getProperties();

SysProps.put("user","userid");

SysProps.put("password","user_password");

Connection conn= DriverManager.getConnection(url, SysProps);

5、Informix數(shù)據(jù)庫

Class.forName("com.informix.jdbc.IfxDriver").newInstance();

String url =

"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;

user=testuser;password=testpassword";

//myDB為數(shù)據(jù)庫名

Connection conn= DriverManager.getConnection(url);

6、MySQL數(shù)據(jù)庫

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

String url ="jdbc:mysql://localhost/myDB?user=softpassword=soft1234useUnicode=truecharacterEncoding=8859_1"

//myDB為數(shù)據(jù)庫名

Connection conn= DriverManager.getConnection(url);

7、PostgreSQL數(shù)據(jù)庫

Class.forName("org.postgresql.Driver").newInstance();

String url ="jdbc:postgresql://localhost/myDB"

//myDB為數(shù)據(jù)庫名

String user="myuser";

String password="mypassword";

Connection conn= DriverManager.getConnection(url,user,password);

JAVA中連接數(shù)據(jù)庫的代碼 請(qǐng)教

private static String url="jdbc:oracle:thin:@localhost:1521:xe";

聲明一個(gè)字符串用于存儲(chǔ)數(shù)據(jù)庫連接信息,jdbc:oracle:thin:@localhost:1521表示你要連接的是oracle數(shù)據(jù)庫地址是本機(jī)

xe為本機(jī)數(shù)據(jù)庫庫名。

private static String driverName="oracle.jdbc.driver.OracleDriver";

這一條是聲明一個(gè)字符串存儲(chǔ)數(shù)據(jù)庫驅(qū)動(dòng)

java怎么和數(shù)據(jù)庫連接

使用java連接MySQL數(shù)據(jù)庫與其他的數(shù)據(jù)庫連接核心是一樣的,如果說區(qū)別,那就是所需的驅(qū)動(dòng)不一樣。

工具/原料

MySQL、JDK

方法/步驟

1、首先需要安裝好JDK(配置環(huán)境變量),如圖所示:

2、其次要安裝好MySQL數(shù)據(jù)庫,可以使用可視化Navicar For MySQL,如圖所示:

3、最后通過代碼進(jìn)行連接。

(1)確定連接路徑URL:

String url="jdbc:mysql://localhost(可以是本機(jī)IP地址):3306(端口號(hào))/mysqltest(數(shù)據(jù)庫名稱)?"+"user=用戶賬號(hào)password=用戶密碼useUnicode=字符編碼";

(2)加載驅(qū)動(dòng):

Class.forName("com.mysql.jdbc.Driver");

(3)連接,獲取Connection對(duì)象

Connection conn=DriverManager.getConnection(url)

(4)可以通過conn對(duì)象檢驗(yàn)連接與否。

名稱欄目:java和數(shù)據(jù)庫連接代碼 javaweb數(shù)據(jù)庫連接代碼
當(dāng)前URL:http://muchs.cn/article18/hjccgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、域名注冊(cè)、做網(wǎng)站、定制網(wǎng)站、、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)