代碼主要列出連接數(shù)據(jù)庫的關(guān)鍵代碼,其他訪問數(shù)據(jù)庫代碼省略
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、吳興網(wǎng)絡(luò)推廣、重慶小程序開發(fā)、吳興網(wǎng)絡(luò)營銷、吳興企業(yè)策劃、吳興品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供吳興建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:muchs.cn
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);
用這個類吧.好的話,給我加加分.
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();
}
}
1. 加載一個對應(yīng)數(shù)據(jù)庫的JDBC驅(qū)動
在建立到一個數(shù)據(jù)庫的連接之前,必須先加載這個數(shù)據(jù)庫的JDBC驅(qū)動程序,加載之后此driver會自動注冊到JDBC驅(qū)動列表中。加載一個JDBC驅(qū)動有兩種方法。
a) 在命令行方式下指定驅(qū)動器或者用冒號分割驅(qū)動器列表:
具體命令如下:
C:\java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject
b)第二種方法,在程序中調(diào)用Class.forName()方法。推薦使用。。。。
try
{
String driverName = “com.imaginary.sql.msql.MsqlDriver”;
Class.forName(driverName).newInstance();
}
Catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
2.連接到數(shù)據(jù)庫。
根據(jù)您后臺待連接的數(shù)據(jù)庫不同,而有小小的差別。
a) 連接到Oracle數(shù)據(jù)庫。
Connection connection = null ;
try
{
//load the jdbc driver ;
String driverName = “oracle.jdbc.driver.OracleDriver”;
Class.forName(driverName).newInstance();
//create a connection to the database;
String serverName = “127.0.0.1”;
String serverPort = “1521”;
String serverID = “datebase1”
String userName = “hello”;
String userPsw = “world”;
String url = “jdbc:oracle.thin:@” + serverName + “:” + serverPort + “:” + serverID ;
Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
b) 連接到一個SQL Server數(shù)據(jù)庫。
Connection connection = null ;
try
{
//load the jdbc driver ;
String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;
Class.forName(driverName).newInstance();
//create a connection to the database;
String serverName = “127.0.0.1”;
String serverPort = “1433”;
String serverID = serverName + serverPort ;
String userName = “hello”;
String userPsw = “world”;
String url = “jdbc:JSQLConnect ://” + serverID ;
Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
c) 連接到一個MySQL數(shù)據(jù)庫上。。。。
Connection connection = null ;
try
{
//load the jdbc driver ;
String driverName = “org.gjt.mm.mysql.Driver”;
Class.forName(driverName).newInstance();
//create a connection to the database;
String serverName = “127.0.0.1”;
String serverID = “database”;
String userName = “hello”;
String userPsw = “world”;
String url = “jdbc:mysql ://” + serverName + “/” + serverID ;
Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
綜合上面的三種數(shù)據(jù)庫連接方式 , 其實大同小異。由于訪問不同的數(shù)據(jù)庫和所使用的數(shù)據(jù)庫驅(qū)動程序不同,所以導(dǎo)致代碼表面上有小小不同,但透過表面看來,內(nèi)部都是
1. 加載一個特定的數(shù)據(jù)庫JDBC驅(qū)動。
2. 連接到一個數(shù)據(jù)庫。
3. 之后,就可以對一個特定的數(shù)據(jù)庫進行特定的操作了。
附上各種數(shù)據(jù)庫的JDBC驅(qū)動起可用信息網(wǎng)址:
對于Oracle數(shù)據(jù)庫,請參考:
對于MySQL數(shù)據(jù)庫,請參考:
對于SQL Server數(shù)據(jù)庫,有很多的驅(qū)動可選,比較常用的:
%
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)錯誤!");
}
%
StringBuffer
sqlq=new
StringBuffer("
SELECT
*
FROM
")
;//申明一個可變字符串
,要存了一個sql語句,并且由"
SELECT
*
FROM
"可知其為一個select查詢語句
sqlq.append(DtoMapGroupOptions.DB_TABLE_NAME)
;//DtoMapGroupOptions.DB_TABLE_NAME應(yīng)該是一個字符串,字面值為一個表的名稱,要在這個表里查數(shù)據(jù)
sqlq.append("
ORDER
BY
")
;//這個制定查出來的結(jié)果集需要排序
sqlq.append(DtoMapGroupOptions.COLUMN_optionID)
;//DtoMapGroupOptions.COLUMN_optionID應(yīng)該是某一列的列名,根據(jù)這一列來排序,如果這一列是數(shù)字,那么就會根據(jù)數(shù)字大小排,字符串可能按abc排,和excel排序時一樣的,即根據(jù)某一列來擴展至整個區(qū)域排序
sqlq.append("
DESC
")
;//這個事制定按降序還是升序,這里是降序
//后面的語句要看上下文,那個pb不知是什么
ListRow
list
=
null
;
pb.isRequireTotalRow(true);
String
sqlStr=sqlq.toString();
list
=
pb.getInfo(sqlStr,
null,
DtoMapGroupOptions.DATA_SOURCE_ID);//可能是把結(jié)果集放入list中,根據(jù)sqlStr中的sql語句
1 將數(shù)據(jù)庫的JDBC驅(qū)動加載到classpath中,在基于JAVAEE的WEB應(yīng)用實際開發(fā)過程中,通常要把目標數(shù)據(jù)庫產(chǎn)品的JDBC驅(qū)動復(fù)制到WEB-INF/lib下.
2 加載JDBC驅(qū)動,并將其注冊到DriverManager中,下面是一些主流數(shù)據(jù)庫的JDBC驅(qū)動加裁注冊的代碼:
//Oracle8/8i/9iO數(shù)據(jù)庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//Sql Server7.0/2000數(shù)據(jù)庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//DB2數(shù)據(jù)庫
Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
//Informix數(shù)據(jù)庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
//Sybase數(shù)據(jù)庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
//MySQL數(shù)據(jù)庫
Class.forName("com.mysql.jdbc.Driver").newInstance();
//PostgreSQL數(shù)據(jù)庫
Class.forNaem("org.postgresql.Driver").newInstance();
3 建立數(shù)據(jù)庫連接,取得Connection對象.例如:
//Oracle8/8i/9i數(shù)據(jù)庫(thin模式)
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String password="tiger";
Connection conn=DriverManager.getConnection(url,user,password);
--完整的太多了!我已經(jīng)把完整的代碼發(fā)到你QQ郵箱了!
當前名稱:java數(shù)據(jù)庫代碼,java調(diào)用數(shù)據(jù)庫代碼
URL地址:http://muchs.cn/article38/hcgosp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、ChatGPT、Google、微信小程序、品牌網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(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)