mysql数据源配置_MySql数据源配置1.tomcat的l中将以下代码写到 前:
name="jdbc/demoDS">
scope="Shareable"
type="javax.sql.DataSource"
factory="at.dbcp.dbcp.BasicDataSourceFactory"
url="jdbc:mysql://localhost:3306/test"
mysql下载配置driverClassName ="sql.jdbc.Driver"
username="root"
password="123456"
/>
l中引⼊数据源(三个参数顺序不能变,否则编译不通过)
jdbc/demoDS
javax.sql.DataSource
Container
3.写java代码连接
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.应⽤⽰例代码,写⼀个查询函数
public List search() throws Exception
{
List result = new LinkedList();
String sql = "SELECT * FROM Stu_Course WHERE SNo='"
+ CNo() +"'";
PreparedStatement pstmt =  conn.prepareStatement(sql); ResultSet rs = uteQuery();
())
{
String[] row = new String[4];
row[0] = rs.getString(1);
row[1] = rs.getString(2);
row[2] = String.Int(3));
row[3] = rs.getString(4);
result.add(row);
}
rs.close();
conn.close();
return result;
}
蛋疼的,以前弄过就忘,再⽤到就有蛋疼⼀遍,记下来。。。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。