java 数据源

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/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.xmlentry might look like this:

<resource-ref>
  <description>
    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 <Context>
    configuration for the web application.
  </description>
  <res-ref-name>
    jdbc/EmployeeDB
  </res-ref-name>
  <res-type>
    javax.sql.DataSource
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>

3、 Code Your Application’s Use Of This Resource

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

4、To configure Tomcat’s resource factory, add an element like this to the <Context>element for the web application.

<Context >
  <Resource name="jdbc/EmployeeDB"
            auth="Container"
            type="javax.sql.DataSource"
            username="dbusername"
            password="dbpassword"
            driverClassName="org.hsql.jdbcDriver"
            url="jdbc:HypersonicSQL:database"
            maxTotal="8"
            maxIdle="4"/>
</Context>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下步骤生成Java数据源的建表DDL: 1. 获取Java数据源的元数据信息,包括表名、列名、数据类型、长度等。 2. 根据元数据信息,生成建表DDL语句。例如,可以使用StringBuilder类构建DDL语句。 3. 将生成的DDL语句输出到控制台或写入文件中。 下面是一个简单的示例代码,仅供参考: ```java import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; public class GenerateDDL { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; try (Connection conn = DriverManager.getConnection(url, user, password)) { DatabaseMetaData metaData = conn.getMetaData(); ResultSet rs = metaData.getColumns(null, null, "my_table", null); StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE my_table ("); while (rs.next()) { String columnName = rs.getString("COLUMN_NAME"); String dataType = rs.getString("TYPE_NAME"); int columnSize = rs.getInt("COLUMN_SIZE"); boolean isNullable = rs.getBoolean("NULLABLE"); sb.append(columnName).append(" ").append(dataType).append("(").append(columnSize).append(")"); if (!isNullable) { sb.append(" NOT NULL"); } sb.append(", "); } sb.delete(sb.length() - 2, sb.length()); sb.append(");"); System.out.println(sb.toString()); } catch (SQLException e) { e.printStackTrace(); } } } ``` 这个例子使用Java的JDBC API来连接MySQL数据库,获取表“my_table”的元数据信息,然后根据元数据信息生成建表DDL语句并输出到控制台。您可以根据自己的需要修改代码以适应不同的数据源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值