Java and database connections

Source: Internet
Author: User
Tags auth resource stmt tomcat

1, with database software (such as: MySQL) to create a database, the database registered with the database name is just the data created

Library, if it is a data source name, ODBC configuration is required.

2, the driver will be placed in the D:\Tomcat 5.5\common\lib or the corresponding development tools of the LIB,

For D:\Tomcat 5.5\conf\web.xml modifications, open Web.xml, add the following in front of </web-app>:

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Navigate through the folder to D:\Tomcat 5.5\conf\catalina\localhost, and find your Web application corresponding to the. XML text

Pieces, such as Root.xml, and add code to the bottom of this file:

<resourcelink name= "Jdbc/mysql" global= "Jdbc/mysql"

Type= "Javax.sql.DataSourcer"/>

Here, the configuration work is basically done.

3, the database connection (note: The following is reproduced)

First, JSP connection oracle8/8i/9i database (with thin mode)

Testoracle.jsp is as follows:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你\");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.