MySQL Data source configuration

Source: Internet
Author: User
Tags auth

The following code will be written to </Host> before the config/server.xml in 1.tomcat:

<context docbase= "Struts1" path= "/struts1" reloadable= "true" source= "Org.eclipse.jst.jee.server:struts1" > <!--struts1 to project name--
<resource
Name= "Jdbc/demods" > <!--name is identical to the name in Web. XML, where Demods is also associated with the following Java code (JAVAX.SQL.DATASOURCE) Ctx.lookup (" Java:/comp/env/jdbc/demods "); partially consistent --
Scope= "Shareable"
Type= "Javax.sql.DataSource"
factory= "Org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
Url= "Jdbc:mysql://localhost:3306/test" <!--test for database name--
Driverclassname = "Com.mysql.jdbc.Driver"
Username= "Root"
Password= "123456"
/>

Data source in 2.web.xml (three parameter order cannot be changed, otherwise compilation does not pass)

<resource-ref>
<res-ref-name>jdbc/demoDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

</resource-ref>

3. Write Java Code Connection

Package util;
Import java.sql.Connection;
public class Struts
{
protected Javax.naming.Context CTX = new Javax.naming.InitialContext ();
protected Javax.sql.DataSource ds;
protected Connection Conn;
Public Struts () throws Exception
{
ds = (Javax.sql.DataSource) ctx.lookup ("Java:/comp/env/jdbc/demods");
conn = Ds.getconnection ();
}
}

4. Apply the sample code and write a query function

Public list<string[]> Search () throws Exception
{
list<string[]> result = new linkedlist<string[]> ();
String sql = "SELECT * from Stu_course WHERE sno= '"
+ FORM.GETCNO () + "'";
PreparedStatement pstmt = conn.preparestatement (sql);
ResultSet rs = Pstmt.executequery ();
while (Rs.next ())
{
string[] row = new String[4];
Row[0] = rs.getstring (1);
ROW[1] = rs.getstring (2);
ROW[2] = string.valueof (Rs.getint (3));
ROW[3] = rs.getstring (4);
Result.add (row);
}
Rs.close ();
Conn.close ();
return result;
}

The egg hurts, once had to forget, again uses has the egg to ache again, writes down ...

MySQL Data source configuration

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.