当前位置: 技术问答>java相关
请问各位朋友:在JAVA的数据库应用管理系统中,如何不在操作系统忠建立数据源便能连接数据库(如ACCESS、MS SQL 等等)
来源: 互联网 发布时间:2015-05-20
本文导语: 请问各位朋友:在JAVA的数据库应用管理系统中,如何不在操作系统中建立数据源便能连接数据库(如ACCESS、MS SQL 等等)?能给个很详细的解答吗?(具体语句是什么?) | 步骤如下: 1. Install Y...
请问各位朋友:在JAVA的数据库应用管理系统中,如何不在操作系统中建立数据源便能连接数据库(如ACCESS、MS SQL 等等)?能给个很详细的解答吗?(具体语句是什么?)
|
步骤如下:
1. Install Your JDBC Driver
Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the $CATALINA_HOME/common/lib directory, which makes the driver available both to the resource factory and to your application.
2. Declare Your Resource Requirements
Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this:
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
jdbc/EmployeDB
javax.sql.DataSource
Container
WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.
3. Code Your Application's Use Of This Resource
A typical use of this resource reference might look like this:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in $CATALINA_HOME/conf/server.xml, as described below.
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the $CATALINA_HOME/conf/server.xml file, nested inside the Context element for this web application (or nested inside a DefaultContext element for the surrounding or element.
...
user
dbusername
password
dbpassword
driverClassName
org.hsql.jdbcDriver
driverName
jdbc:HypersonicSQL:database
...
Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor. Customize the value of the mail.smtp.host parameter to point at the server that provides SMTP service for your network.
This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.
1. Install Your JDBC Driver
Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the $CATALINA_HOME/common/lib directory, which makes the driver available both to the resource factory and to your application.
2. Declare Your Resource Requirements
Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this:
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
jdbc/EmployeDB
javax.sql.DataSource
Container
WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.
3. Code Your Application's Use Of This Resource
A typical use of this resource reference might look like this:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();
Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in $CATALINA_HOME/conf/server.xml, as described below.
4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the $CATALINA_HOME/conf/server.xml file, nested inside the Context element for this web application (or nested inside a DefaultContext element for the surrounding or element.
...
user
dbusername
password
dbpassword
driverClassName
org.hsql.jdbcDriver
driverName
jdbc:HypersonicSQL:database
...
Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor. Customize the value of the mail.smtp.host parameter to point at the server that provides SMTP service for your network.
This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.
|
不用ODBC不就行了么?
直接找JDBC driver,就可以了啊
不过,好像网上的JDBC driver大部分是要钱的
免费的也有,我用过,可是网站名忘了,自己嗖嗖吧
直接找JDBC driver,就可以了啊
不过,好像网上的JDBC driver大部分是要钱的
免费的也有,我用过,可是网站名忘了,自己嗖嗖吧
|
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();