A. Java中如何實現與後台資料庫的連接
用JAVA連接資料庫主要有兩種方式,一是用JDBC-ODBC橋來連接,二是用相關廠商提供的相應驅動程序來連接,首先談談第一種連接。
JDBC-ODBC橋接器是用JdbcOdbc.Class和一個用於訪問ODBC驅動程序的本地庫實現的。對於WINDOWS平台,該本地庫是一個動態連接庫DLL(JDBCODBC.DLL)。
由於JDBC在設計上與ODBC很接近。在內部,這個驅動程序把JDBC的方法映射到ODBC調用上,這樣,JDBC就可以和任何可用的ODBC驅動程序進行交互了。這種橋接器的優點是,它使JDBC目前有能力訪問幾乎所有的資料庫。通行方式如圖所示:
應用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC層---數據源
具體操作方法為:
首先打開控制面板的管理工具,打開數據源(ODBC),在用戶DSN裡面添加數據源(即你要連接的資料庫的名字),在這里假定連接sql SERVER 2000的GoodsSupply資料庫。名稱填寫你要連接的資料庫的名稱(GoodsSupply),然後逐步設置,如果選用了使用SQL-SERVER密碼認證的話,就要輸入相應的用戶名及密碼連接到資料庫。一路下一步設置完成。
在JAVA裡面編寫程序進行測試,在這里我的程序是讓用戶輸入任意的表名與與列名,把該列的所有數據輸出。源代碼如下:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;
public class ODBCBridge {
public static void main(String[] args) {
String url="jdbc:odbc:GoodsSupply";
Statement sm=null;
String command=null;
ResultSet rs=null;
String tableName=null;
String cName=null;
String result=null;
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //載入驅動
}catch(ClassNotFoundException e){
System.out.println("Can not load Jdbc-Odbc Bridge Driver");
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
Connection con=DriverManager.getConnection(url,"USER","PASSWORD"); //使用SQL-SERVER2000認證
DatabaseMetaData dmd=con.getMetaData(); //DMD為連接的相應情況
System.out.println("連接的資料庫:"+dmd.getURL());
System.out.println("驅動程序:"+dmd.getDriverName());
sm=con.createStatement();
System.out.println("輸入表名");
tableName=input.readLine();
while(true) {
System.out.println("輸入列名(為空時程序結束):");
cName=input.readLine();
if(cName.equalsIgnoreCase(""))
break;
command="select "+cName+" from "+tableName;
rs=sm.executeQuery(command); //執行查詢
if(!rs.next())
System.out.println("表名或列名輸入有誤");
else {
System.out.println("查詢結果為:");
do
{
result=rs.getString(cName);
//資料庫語言設置為中文,不用轉換編碼
//result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
}
}catch(SQLException ex) {
System.out.println("SQLException:");
while(ex!=null) {
System.out.println("Message:"+ex.getMessage());
ex=ex.getNextException();
}
}catch(Exception e) {
System.out.println("IOException");
}
}
}
B. Java中如何與資料庫建立連接
導入java.sql包
一、載入要連接資料庫的驅動程序
//Jdbc-Odbc橋 和 Microsoft Access 資料庫
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// SQL Server 驅動程序:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
註:Class.forName()方法將給定的類載入到JVM,如果系統中不存在給定的類,則會引發異常
二、通過驅動程序管理器得到連接實例
Connection conn=null;
//1.
//1.1建立數據源
conn=DriverManager.getConnection("jdbc:odbc:MyDataSource"); //MyDataSource是數據源名稱
//1-2、不建立數據源
conn=DriverManager.getConnection("jdbc:odbc:;Driver=Microsoft Access Driver (*.mdb);DBQ=C:\\VBTest.mdb");
//2.SQL Server
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=mydb","sa","");
註:DriverManager類跟蹤已注冊的驅動程序,通過getConnection(URL)方法, 找到一個能夠連接至URL中指定的資料庫驅動程序
它接收三個參數, 分別表示1 數據源的名稱、類型 2 用戶名(可選) 3 密碼(可選)
三、基於連接對象建立處理器對象
Statement stmt=conn.createStatement();
四、准備sql命令
String sql="select * from Student";
五、執行命令返回結果集
ResultSet rs=stmt.executeQuery(sql);
六、顯示結果集
while(rs.next())//只要後面有記錄
{
//對當前行的所有欄位遍歷
for(int i=1;i<=rs.getMetaData().getColumnCount();i++)
{
System.out.print(rs.getMetaData().getColumnName(i)+": ");//顯示欄位名
System.out.println(rs.getString(i));//顯示欄位當前值
}
System.out.println();
}
七、關閉資源
rs.close(); //關閉記錄集
stmt.close(); //關閉處理器對象
conn.close(); //關閉連接對象
預處理器的應用:
//3.基於連接對象建立預處理器對象
PreparedStatement pstmt=conn.prepareStatement("insert into student values(?,?,?,?)");
//4.給預處理對象的參數賦值
pstmt.setString(1,"8888");
pstmt.setString(2,"nemo");
pstmt.setString(3,"accp");
pstmt.setString(4,"sanxianglu");
//5.執行預處理命令
int i=pstmt.executeUpdate();
System.out.println(i+"條記錄已成功插入!");
C. java 如何連接資料庫
各種資料庫使用JDBC連接的方式:
1、Oracle8/8i/9i資料庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl為資料庫的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
2、DB2資料庫
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample"; //sample為你的資料庫名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
3、Sql Server7.0/2000資料庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb為資料庫
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
4、Sybase資料庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";//myDB為你的資料庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5、Informix資料庫
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為資料庫名
Connection conn= DriverManager.getConnection(url);
6、MySQL資料庫
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB為資料庫名
Connection conn= DriverManager.getConnection(url);
7、PostgreSQL資料庫
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB" //myDB為資料庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
8、access資料庫直連用ODBC的
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
String url="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/ReportDemo.mdb");
Connection conn = DriverManager.getConnection(url,"","");
Statement stmtNew=conn.createStatement() ;
D. java應用程序怎麼和資料庫進行連接
1、知道與資料庫的連接方式
2、有第三方的資料庫驅動包
3、編寫對應的驅動連接代碼
就拿MySQL資料庫舉個例子,這些網上都是有實例的,比如這篇博客就說的非常的清楚。
http://www.cnblogs.com/soplayer/archive/2007/06/26/796565.html
希望以上回答對你有所幫助,
E. 怎麼使用JAVA連接資料庫
1、首先我們先建好資料庫,然後建立好程序的目錄,因為是適用於初學者的,所以就建立一個簡單的java project,如圖。
F. 在java中可以用幾種方法連接資料庫
你的問題很難回答啊
如果從內在原理來講就是一種:先載入驅動程序再獲得連接
如果從操作來講我把他分三種:
1,直接在你的代碼中用代碼寫出載入驅動和獲得連接的代碼
2,在windows的數據源中配置一個數據源,這種方法一般不用
3,如果是網站開發可以在web應用的web.xml中配置一個連接池,用時直接從池中獲得連接.
java與資料庫的連接都是通過JDBC介面實現的你如果要問的是JDBC的種類的話你可以查數就4類
1,jdbc-odbc橋
2,jdbc-native方法
3,jdbc-網路
4,jdbc驅動
你如果問JDBC具體有哪些,那隻能告訴你有幾種資料庫就有幾種JDBC.