在jsp中獲得數(shù)據(jù)源的方法是通過jdbc或者datasource連接到數(shù)據(jù)庫然后獲取得到。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了安丘免費建站歡迎大家使用!
1、jsp頁面代碼:
%@page contentType="text/html" pageEncoding="UTF-8"%
%@taglib prefix="c" uri=""%
jsp:useBean id="db" class="cc.openhome.DatabaseBean"/
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
""
html
head
meta http-equiv="Content-Type"
content="text/html; charset=UTF-8"
title測試數(shù)據(jù)庫連接/title
/head
body
c:choose
c:when test="${db.connectedOK}"連接成功!/c:when
c:otherwise連接失敗!/c:otherwise
/c:choose
/body
/html
2、在tomcat中配置
JDBCDemo context.xml
?xml version="1.0" encoding="UTF-8"?
Context antiJARLocking="true" path="/JDBCDemo"
Resource name="jdbc/demo"
auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root"
password="123456" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/demo?
useUnicode=truecharacterEncoding=UTF8"/
/Context
3、在java后臺實現(xiàn)數(shù)據(jù)源的連接
import java.io.Serializable;import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DatabaseBean implements Serializable {
private DataSource dataSource;
public DatabaseBean() {
try {
Context initContext = new InitialContext();
Context envContext = (Context)
initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/demo");
} catch (NamingException ex) {
throw new RuntimeException(ex);
}
}
public boolean isConnectedOK() {
boolean ok = false;
Connection conn = null;
SQLException ex = null;
try {
conn = dataSource.getConnection();
if (!conn.isClosed()) {
ok = true;
}
} catch (SQLException e) {
ex = e;
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
if(ex == null) {
ex = e;
}
}
}
if(ex != null) {
throw new RuntimeException(ex);
}
}
return ok;
}
}
jdbc數(shù)據(jù)庫連接:1.加載驅(qū)動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)會返回查詢結(jié)果集,stmt.executeUpdate(String sql)返回int型,表示影響記錄的條數(shù);5處理結(jié)果:ResultSet rs=str.executeQuery(String sql);while(rs.next()){
System.out.println(rs.getInt(id));
}
5:關(guān)閉數(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è)置為數(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è)置數(shù)據(jù)源
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();
你是使用JNDI獲得數(shù)據(jù)源對象的嗎?如果是:
1,、配置context.xml文件
Context
Resource name="jdbc/news"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="jbit"
password="dbqn"'
driverClassName="Oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:daataName"
/Context
以上用的是Oracle數(shù)據(jù)庫的驅(qū)動和url
你可以換成SQL的
2、配置web.xml文件
resource-ref
res-authContainer/res-auth
res-typejavax.sql.DataSource/res-type
res-ref-namejdbc/news/res-ref-name
/resource-ref
3、添加數(shù)據(jù)庫驅(qū)動
4、在javaBean中編寫代碼,使用lookup()方法獲得數(shù)據(jù)源對象
public class BaseDAO {
private static Connection conn = null;
public static Connection getConnection() {
try {
Context context = new InitialContext();
DataSource ds = (DataSource) context
.lookup("java:comp/env/jdbc/news");
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
注:lookup("java:comp/env/jdbc/news");
中的參數(shù)"java:comp/env/"+數(shù)據(jù)源名稱的形式
如果你要的話我正好有這Struts2的項目我可以給你看看
詳情如下:
以JNDI方式創(chuàng)建數(shù)據(jù)源首先要配置數(shù)據(jù)源的相關(guān)連接信息,也就是數(shù)據(jù)源連接池。該配置應(yīng)該在Tomcat安裝目錄下的conf/context.xml 文件中配置,在Eclipse的J2EE架構(gòu)下,也可以把context.xml文件創(chuàng)建在/META-INF目錄下。
正確的配置后,就可以在程序中以JNDI的方式創(chuàng)建數(shù)據(jù)源,得到數(shù)據(jù)庫連接并進(jìn)行相應(yīng)的操作。
新聞標(biāo)題:java代碼獲得數(shù)據(jù)源,java如何獲取數(shù)據(jù)源
網(wǎng)站網(wǎng)址:http://chinadenli.net/article45/dsidhhi.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、定制網(wǎng)站、App設(shè)計、云服務(wù)器、網(wǎng)站制作、ChatGPT
聲明:本網(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)