數(shù)據(jù)庫的訪問java代碼 數(shù)據(jù)庫的訪問java代碼

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

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

創(chuàng)新互聯(lián)長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為察哈爾右翼前企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、成都網(wǎng)站制作,察哈爾右翼前網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

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連接sqlserver數(shù)據(jù)庫代碼

%

String sqlDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";

String url="jdbc:sqlserver://localhost:1433;DatabaseName=自己的數(shù)據(jù)庫名字";

String user="用戶名";

String password="密碼";

Connection conn=null;

try{

Class.forName(sqlDriver).newInstance();

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

// out.println("數(shù)據(jù)庫加載成功");

}catch(Exception e){

// out.println("數(shù)據(jù)庫加載出現(xiàn)錯誤!");

}

%

java如何訪問數(shù)據(jù)庫

Java可以使用JDBC訪問數(shù)據(jù)庫,也可以使用各類ORM框架訪問數(shù)據(jù)庫,但這些框架最終還是通過JDBC訪問數(shù)據(jù)庫,它們只是封裝了數(shù)據(jù)庫操作,而使得開發(fā)者可以減少這部分消耗。因此,本文只講解JDBC訪問方式。

JDBC訪問一般分為如下流程:

1、加載JDBC驅(qū)動程序:

在連接數(shù)據(jù)庫之前,首先要加載想要連接的數(shù)據(jù)庫的驅(qū)動到JVM(Java虛擬機(jī)),

這通過java.lang.Class類的靜態(tài)方法forName(String className)實現(xiàn)。

例如:

try{

//加載MySql的驅(qū)動類

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

}catch(ClassNotFoundException e){

System.out.println("找不到驅(qū)動程序類 ,加載驅(qū)動失??!");

e.printStackTrace() ;

}

成功加載后,會將Driver類的實例注冊到DriverManager類中。

2、提供JDBC連接的URL

?連接URL定義了連接數(shù)據(jù)庫時的協(xié)議、子協(xié)議、數(shù)據(jù)源標(biāo)識。

?書寫形式:協(xié)議:子協(xié)議:數(shù)據(jù)源標(biāo)識

協(xié)議:在JDBC中總是以jdbc開始

子協(xié)議:是橋連接的驅(qū)動程序或是數(shù)據(jù)庫管理系統(tǒng)名稱。

數(shù)據(jù)源標(biāo)識:標(biāo)記找到數(shù)據(jù)庫來源的地址與連接端口。

例如:(MySql的連接URL)

jdbc:mysql://localhost:3306/test?useUnicode=truecharacterEncoding=gbk ;

useUnicode=true:表示使用Unicode字符集。如果characterEncoding設(shè)置為

gb2312或GBK,本參數(shù)必須設(shè)置為true 。characterEncoding=gbk:字符編碼方式。

3、創(chuàng)建數(shù)據(jù)庫的連接

?要連接數(shù)據(jù)庫,需要向java.sql.DriverManager請求并獲得Connection對象,該對象就代表一個數(shù)據(jù)庫的連接。

?使用DriverManager的getConnectin(String url,String username,String password )方法傳入指定的欲連接的數(shù)據(jù)庫的路徑、數(shù)據(jù)庫的用戶名和密碼來獲得。

例如:

//連接MySql數(shù)據(jù)庫,用戶名和密碼都是root

String url = "jdbc:mysql://localhost:3306/test" ;

String username = "root" ;

String password = "root" ;

try{

Connection con =

DriverManager.getConnection(url , username , password ) ;

}catch(SQLException se){

System.out.println("數(shù)據(jù)庫連接失敗!");

se.printStackTrace() ;

}

4、創(chuàng)建一個Statement

?要執(zhí)行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3種類型:

1、執(zhí)行靜態(tài)SQL語句。通常通過Statement實例實現(xiàn)。

2、執(zhí)行動態(tài)SQL語句。通常通過PreparedStatement實例實現(xiàn)。

3、執(zhí)行數(shù)據(jù)庫存儲過程。通常通過CallableStatement實例實現(xiàn)。

具體的實現(xiàn)方式:

Statement stmt = con.createStatement() ;

PreparedStatement pstmt = con.prepareStatement(sql) ;

CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ;

5、執(zhí)行SQL語句

Statement接口提供了三種執(zhí)行SQL語句的方法:executeQuery 、executeUpdate和execute

1、ResultSet executeQuery(String sqlString):執(zhí)行查詢數(shù)據(jù)庫的SQL語句,返回一個結(jié)果集(ResultSet)對象。

2、int executeUpdate(String sqlString):用于執(zhí)行INSERT、UPDATE或DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等

3、execute(sqlString):用于執(zhí)行返回多個結(jié)果集、多個更新計數(shù)或二者組合的語句。

具體實現(xiàn)的代碼:

ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;

int rows = stmt.executeUpdate("INSERT INTO ...") ;

boolean flag = stmt.execute(String sql) ;

6、處理結(jié)果

兩種情況:

1、執(zhí)行更新返回的是本次操作影響到的記錄數(shù)。

2、執(zhí)行查詢返回的結(jié)果是一個ResultSet對象。

?ResultSet包含符合SQL語句中條件的所有行,并且它通過一套get方法提供了對這些行中數(shù)據(jù)的訪問。

?使用結(jié)果集(ResultSet)對象的訪問方法獲取數(shù)據(jù):

while(rs.next()){

String name = rs.getString("name") ;

String pass = rs.getString(1); // 此方法比較高效(列是從左到右編號的,并且從列1開始)

}

7、關(guān)閉JDBC對象

操作完成以后要把所有使用的JDBC對象全都關(guān)閉,以釋放JDBC資源,關(guān)閉順序和聲明順序相反:

1、關(guān)閉記錄集

2、關(guān)閉聲明

3、關(guān)閉連接對象

if(rs != null){ // 關(guān)閉記錄集

try{

rs.close() ;

}catch(SQLException e){

e.printStackTrace() ;

}

}

if(stmt != null){ // 關(guān)閉聲明

try{

stmt.close() ;

}catch(SQLException e){

e.printStackTrace() ;

}

}

if(conn != null){ // 關(guān)閉連接對象

try{

conn.close() ;

}catch(SQLException e){

e.printStackTrace() ;

}

}

如何在Java程序中訪問mysql數(shù)據(jù)庫中的數(shù)據(jù)并進(jìn)行簡單的操作

給你一段參考代碼:

package 數(shù)據(jù)庫編程;

import java.sql.*;

public class 數(shù)據(jù)庫連接 {

public static void main(String[] args) throws SQLException{

//1.加載驅(qū)動程序

try {

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

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//2.創(chuàng)建與DB數(shù)據(jù)庫的連接

//連接字符串

String url="jdbc:mysql://192.168.1.101:3306/java數(shù)據(jù)庫?user=rootpassword=humin";

Connection con=DriverManager.getConnection(url);

//進(jìn)行讀寫

if(!con.isClosed()){

System.out.print("歡迎訪問我的數(shù)據(jù)庫!\n你想做什么啊………………\n");

Statement st=con.createStatement();

ResultSet rs= st.executeQuery("select * from stu");

while(rs.next()){

System.out.print( rs.getString("stuid")+","+rs.getString("name")+","+rs.getString("sex")+","+rs.getString("age")+","+rs.getString("address")+","+rs.getString("tel")+"\n");

}

}

//關(guān)閉數(shù)據(jù)庫

con.close();

}

}

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

用這個類吧.好的話,給我加加分.

import java.sql.*;

/**

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

* @作者: 李開歡

* @日期: 2007/

*/

public class ConnectionDemo {

/*

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

*/

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("編號 "+"類 型");

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

}

}

網(wǎng)站名稱:數(shù)據(jù)庫的訪問java代碼 數(shù)據(jù)庫的訪問java代碼
標(biāo)題網(wǎng)址:http://muchs.cn/article30/hjsiso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、搜索引擎優(yōu)化、網(wǎng)頁設(shè)計公司、網(wǎng)站營銷微信公眾號、小程序開發(fā)

廣告

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

微信小程序開發(fā)