您的位置:首页 > 编程语言 > Java开发

运用java进行web开发时常用的连接数据库方式

2012-03-26 21:04 531 查看
jndi(spring,也需通过weblogic部署)

weblogic部署(jdbc,web.xml加载配置文件,包含jndi)

jdbc/spring加载配置文件

hibernate-spring/ibatis-spring

1.      jdbc

<body>
    <%
    Class.forName ("com.mysql.jdbc.Driver");

    Connection conn =DriverManager.getConnection("jdbc:mysql://localhost:3306/addressbook","root","");
    Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select* from USERS");
(或者:PreparedStatement pstmt=conn. prepareStatement(("select* fromUSERS"))
    %>
    <table
bgcolor="9999dd"border="l">
    <%
    while(rs.next())
    {
     %>
     <tr>
    <td><%=rs.getString(1)%></td>
    <td><%=rs.getString(2)%></td>
    <td><%=rs.getString(3)%></td>
    <td><%=rs.getString(4)%></td>
    <td><%=rs.getString(5)%></td>
    </tr>
    <%}
%>
 
     <table>
  </body>

 

2.    在web.xml中配置加载配置文件。



Web.xml中:



db_conn_pro.properties中:
#init the param for DB connection

#Tomcat-java:comp/env/[DBCON_TYPE=JNDI,java:comp/env/serverin_jcxh]

DBCON_TYPE=JNDI,CMSJNDI

#DBCON_URL=t3://10.153.195.55:7788

#DBCON_USER=

#DBCON_PASS=

CMSJNDI需要在weblogic中进行配置。

3.      通过spring,在web.xml中加载spring配置文件,通过在spring配置文件中配置jndi

Web.xml中:



Spring配置文件中:



或者:



Jdbc_produce需要在weblogic中配置

 

4.      通过spring加载配置文件

Web.xml中:



Spring配置文件中:

<bean id="propertyConfigurer" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
      <value>classpath:jdbc.properties</value>
   </property>
</bean>
jdbc.properties文件中:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐