java指定數(shù)據(jù)源的代碼 java 數(shù)據(jù)源

java 用程序創(chuàng)建數(shù)據(jù)源怎么做

jdbc數(shù)據(jù)庫連接:1.加載驅動Class.forName(“xxxDriver”)2建立連接:Connection conn= DriverManager.getConnection(url,user,password);(url是連接地址ip端口號和數(shù)據(jù)庫實例名,user用戶名,password密碼)3獲取statement對象:Statement stmt=conn.createStatement();4通過Statement執(zhí)行Sql語句:stmt.executeQquery(String sql)會返回查詢結果集,stmt.executeUpdate(String sql)返回int型,表示影響記錄的條數(shù);5處理結果:ResultSet rs=str.executeQuery(String sql);while(rs.next()){

成都網(wǎng)絡公司-成都網(wǎng)站建設公司創(chuàng)新互聯(lián)建站十載經(jīng)驗成就非凡,專業(yè)從事成都網(wǎng)站設計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設,成都網(wǎng)頁設計,成都網(wǎng)頁制作,軟文營銷,廣告投放平臺等。十載來已成功提供全面的成都網(wǎng)站建設方案,打造行業(yè)特色的成都網(wǎng)站建設案例,建站熱線:028-86922220,我們期待您的來電!

System.out.println(rs.getInt(id));

}

5:關閉數(shù)據(jù)源:rs.close();

下面是連接各種數(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);

8、JDBC-ODBC橋

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:jsp");

jsp為建立的odbc數(shù)據(jù)源名,事先要先將SQL server的表設置為數(shù)據(jù)源。在“管理工具”-“數(shù)據(jù)源odbc”里用系統(tǒng)DNS添加。

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

//import java.sql.*;

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

Statement stmtNew=conn.createStatement();

9.DB2數(shù)據(jù)庫

//import java.sql.*;

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

Statement stmtNew=conn.createStatement();

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

//import java.sql.*;

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

//String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db2"; //7.0、2000

String url="jdbc:sqlserver://localhost:1433;DatabaseName=db2"; //2005

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

String user="sa";

String password="";

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

Statement stmtNew=conn.createStatement();

11.Sybase數(shù)據(jù)庫

//import java.sql.*;

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

Statement stmtNew=conn.createStatement();

12.Informix數(shù)據(jù)庫

//import java.sql.*;

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

Statement stmtNew=conn.createStatement();

13.MySQL數(shù)據(jù)庫

//import java.sql.*;

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

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

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

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

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

Connection conn= DriverManager.getConnection(url,"root","root");

Statement stmtNew=conn.createStatement();

14.PostgreSQL數(shù)據(jù)庫

//import java.sql.*;

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

Statement stmtNew=conn.createStatement();

15.access數(shù)據(jù)庫直連用ODBC的

//import java.sql.*;

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,"sa","");

Statement stmtNew=conn.createStatement();

16.程序計時

long time1=System.currentTimeMillis();

long time2=System.currentTimeMillis();

long interval=time2-time1;

17.延時

try {

Thread.sleep(Integer.Parse(%%1));

} catch(InterruptedException e) {

e.printStackTrace();

}

18.連接Excel文件

//import java.sql.*;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ=D:\\myDB.xls"; // 不設置數(shù)據(jù)源

String user="myuser";

String password="mypassword";

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

Statement stmtNew=conn.createStatement();

求助:Java數(shù)據(jù)源怎么配置?

一 首先要配置Tomcat的server.xml文件,在對應的web應用的Context中加入Resource元素,比如:

Context path="/Manager" reloadable="true"

Resource

name="hello"

type="javax.sql.DataSource"

driverClassName="com.mysql.jdbc.Driver"

username="root"

password="123456"

maxIdle="4"

maxActive="4"

maxWait="5000"

url="jdbc:mysql://127.0.0.1/jspdev"

/

/Context

其中:

name:指定Resource的JNDI名字

type:指定Resource所屬的Java類名

driverClassName:指定連接數(shù)據(jù)庫的JDBC驅動程序

username:指定連接數(shù)據(jù)庫的用戶名

password:指定連接數(shù)據(jù)庫的口令

maxIdle:指定數(shù)據(jù)庫連接池中的最大空閑連接數(shù)目,0表示不受限制

maxActive:指定數(shù)據(jù)庫連接池中的最大活動連接數(shù)目,0表示不受限制

maxWait:指定連接池中連接處于空閑狀態(tài)的最長時間,超過會拋出異常,-1表示無限

url:指定連接數(shù)據(jù)庫的URL

二 在Web應用中使用數(shù)據(jù)源:

javax.naming.Context提供了查找JNDI Resource的接口,可以通過三個步驟來使用數(shù)據(jù)源對象:

A.獲得對數(shù)據(jù)源的引用:

Context ctx = new InitalContext();

DataSource ds =

(DataSource)ctx.lookup("java:comp/env/hello");

B.獲得數(shù)據(jù)庫連接對象:

Connection con = ds.getConnection();

C.返回數(shù)據(jù)庫連接到連接池:

con.close();

在連接池中使用close()方法和在非連接池中使用close()方法的區(qū)別是:前者僅僅是把數(shù)據(jù)庫連接對象返回到數(shù)據(jù)庫連接池中,是連接對象又恢復到空閑狀態(tài),而非關閉數(shù)據(jù)庫連接,而后者將直接關閉和數(shù)據(jù)庫的連接。

三 如果通過數(shù)據(jù)源訪問數(shù)據(jù)庫,由于數(shù)據(jù)源由Servlet容器創(chuàng)建并維護,所以必須把JDBC驅動程序拷貝到Tomcat安裝目錄下的common/lib目錄下,確保Servlet容器能夠訪問驅動程序。

java中spring配置中連接數(shù)據(jù)庫的代碼怎么寫?

!-- 配置數(shù)據(jù)源 --

bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"

property name="driverClassName" value="com.mysql.jdbc.Driver"/property

property name="url" value="jdbc:mysql://192.168.2.100:3306/test"/property

property name="username" value="root"/property

property name="password" value="root"/property

/bean

!-- 配置SessionFactory并注入其依賴的數(shù)據(jù)源 --

bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"

property name="dataSource"

ref bean="dataSource" /!-- 注入一個數(shù)據(jù)源 --

/property

!-- 配置Hibernate的屬性 --

property name="hibernateProperties"

props

prop key="hibernate.dialect"org.hibernate.dialect.MySQLDialect/prop

prop key="hibernate.show_sql"true/prop

/props

/property

!-- 配置hibernate自動加載持久化映射文件 --

property name="mappingResources"

list

valuedomain/Person.hbm.xml/value/list

/property

/bean

新聞名稱:java指定數(shù)據(jù)源的代碼 java 數(shù)據(jù)源
URL分享:http://bm7419.com/article22/ddcogjc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、App開發(fā)網(wǎng)站收錄、、電子商務、小程序開發(fā)

廣告

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

h5響應式網(wǎng)站建設