package com.Dao;

創(chuàng)新互聯(lián)主營城中網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),城中h5微信小程序定制開發(fā)搭建,城中網(wǎng)站營銷推廣歡迎城中等地區(qū)企業(yè)咨詢
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
public class DbHelper {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost;databasename=db_Blog";
private static final String UID="sa";
private static final String PWD="sasa";
public static Connection getConnection(){
Connection con=null;
try {
Class.forName(DRIVER);
con=DriverManager.getConnection(URL,UID,PWD);
System.out.println("連接數(shù)據(jù)庫成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("sb");
}
return con;
}
public static void main(String []p){
getConnection();
}
public static Result runSelectSql(String sql){
Connection con=null;
PreparedStatement pst=null;
ResultSet res=null;
Result result=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
res=pst.executeQuery();
result=ResultSupport.toResult(res);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return result;
}
public static Result runSelectSql(String sql,Object[] params){
Connection con=null;
PreparedStatement pst=null;
ResultSet res=null;
Result result=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
for(int i=0;iparams.length;i++){
pst.setObject(i+1,params[i]);
}
res=pst.executeQuery();
result=ResultSupport.toResult(res);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return result;
}
public static boolean runInset(String sql,Object[] params){
boolean flag=false;
Connection con=null;
PreparedStatement pst=null;
try {
con=getConnection();
pst=con.prepareStatement(sql);
for(int i=0;iparams.length;i++){
pst.setObject(i+1,params[i]);
}
int a=pst.executeUpdate();
if(a0){
flag=true;
}
else{flag=false;}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
try {
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
return flag;
}
}
這是以前我們課上寫的 你看看
package com.bin.struts.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn2000 {
public static Connection getConn(){
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=//數(shù)據(jù)庫名字";
String user = "sa";
String password = "123";
try {
Class.forName(driver);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
return DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static Connection close(){
return null;
}
}
這是2000的連接方法
這是2005的連接串
jdbc:sqlserver://localhost:1433;databasename=//數(shù)據(jù)庫
com.microsoft.sqlserver.jdbc.SQLServerDriver
2000數(shù)據(jù)庫需要打SP4補丁.
并且兩個都需要專門的JDBC驅(qū)動的
可能是我孤弱寡聞,我認(rèn)為這是不可能的,首先你要了解什么是ODBC:
ODBC 是 Microsoft的一項技術(shù),是開放式數(shù)據(jù)庫互連(Open Database Connectivity)的縮寫,這是一族API,與Windows API相似。它主要與數(shù)據(jù)庫打交道。就是說,利用ODBC API,你可通過統(tǒng)一界面和好多各不相同的數(shù)據(jù)庫打交道。
ODBC有四個組成部分:
應(yīng)用程序 (Application,你的程序)
ODBC 管理器 (ODBC manager)
ODBC 驅(qū)動程序(ODBC Drivers)
數(shù)據(jù)源 (Data Sources,數(shù)據(jù)庫)
這四個組件的核心是ODBC 管理器。 你可把它想象成你的監(jiān)工。你告訴它你希望他作什么,然后它把你的要求傳達給它的工人(ODBC 驅(qū)動程序)并完成工作。如果工人有什么想告訴你的,它會與監(jiān)工(ODBC 管理器)說,由監(jiān)工傳達給你。工人們很明白他們應(yīng)作什么,因此他們會為你很好的完成工作。
JDBC-ODBC連接橋正是通過驅(qū)動把程序和ODBC連接起來,調(diào)用ODBC管理器訪問數(shù)據(jù)庫,所以怎么在程序里設(shè)置ODBC呢?這就好像你在自己家看電視,卻想控制電視臺的衛(wèi)星接收器接收節(jié)目一樣了。
實現(xiàn)思路:就是通過ojdbc.jar中提供的方法,直接連接數(shù)據(jù)庫即可,固定代碼寫法如下:
import?java.sql.Connection;
import?java.sql.DriverManager;
import?java.sql.ResultSet;
import?java.sql.Statement;
public?class?JDBCTest?{
public?static?void?main(String[]?args)?throws?Exception?{
//1.加載驅(qū)動
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.創(chuàng)建數(shù)據(jù)庫連接對象
//Connection?conn?=?DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db","sa","sqlpass");
//Connection?conn?=?DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=truecharacterEncoding=UTF-8","root","123456");
Connection?conn?=?DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","Oracle123");
//3.創(chuàng)建數(shù)據(jù)庫命令執(zhí)行對象
Statement?stmt?=?conn.createStatement();
//??????PreparedStatement?ps?=?conn.prepareStatement("select?*?from?t_user");
//4.執(zhí)行數(shù)據(jù)庫命令
ResultSet?rs?=?stmt.executeQuery("select?*?from?t_user");
//??????ResultSet?rs?=?ps.executeQuery();
//5.處理執(zhí)行結(jié)果
while?(rs.next())?{
int?id?=?rs.getInt("id");
String?username?=?rs.getString("username");
String?password?=?rs.getString("password");
System.out.println(id?+?"\t"?+?username?+?"\t"?+?password);
}
//6.釋放數(shù)據(jù)庫資源
if?(rs?!=?null)?{
rs.close();
}
//??????if?(ps?!=?null)?{
//??????????ps.close();
//??????}
if?(stmt?!=?null)?{
stmt.close();
}
if?(conn?!=?null)?{
conn.close();
}
}
}
網(wǎng)站欄目:Javaodbc代碼 javadoc @code
網(wǎng)站鏈接:http://chinadenli.net/article4/dohgsoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、軟件開發(fā)、網(wǎng)站導(dǎo)航、定制開發(fā)、動態(tài)網(wǎng)站、搜索引擎優(yōu)化
聲明:本網(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)