日期:2014-05-20  浏览次数:20815 次

myeclipse连接mysql问题。
已经建了context.xml文件
<?xml vertion="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<resource name="jdbc/mydb" auth="Container"
type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?autoReconnect=true"
username="root" password="capsa"
maxActive="100" maxIdle="30" maxWait="10000"/>
</Context>
连接mysql时还需要其他配置吗?我用的是myeclipse9.0
在调用时显示null.
public static void main(String[] args) {
// TODO Auto-generated method stub
  DBConnection dbConnection = new DBConnection();
  Connection connection = dbConnection.getConnection();
  System.out.print(connection);
}

  

public Connection getConnection()
{
Connection conn = null;
Context ctx;
  try 
  {
  ctx = new InitialContext();
  DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/mydb");
  conn = dataSource.getConnection();
  } catch (SQLException e)
{
e.printStackTrace();

} catch (NamingException e) {



return conn;
   
}
在线等,希望高手帮忙看一下啊~~~~QQ:1658535306

------解决方案--------------------
首先我们在context.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/MusicPro">
<Resource
name="jdbc/myDS"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
password="longke"
url="jdbc:mysql://127.0.0.1:3306/video"
maxActive="4"/>
</Context>

web.xml中
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

最后别忘了把mysql驱动器拷贝到tomcat的lib目录下