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