但是在java类里面不能使用啊!!!javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
如果拿不到这个conn 那么写的DAO包下的数据库的CRUD等等操作都用不了啊
如何使用这个连接池啊????
8 个解决方案
#1
这有深入的介绍
http://hihitiger.iteye.com/blog/899388
http://hihitiger.iteye.com/blog/899388
#2
你说的太模糊了
贴出的java连接数据库的代码
贴出的java连接数据库的代码
#3
JSP代码,成功!<%
Connection conn = null;
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/sqlserver");//获取连接池对象
DataSource ds = (javax.sql.DataSource) obj; //类型转换
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println(rs.getString(1) + "<BR>");
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功");
%>
java代码 失败
public static void main(String[] args) {
try {
InitialContext ict= new InitialContext();
DataSource ds =(DataSource)ict.lookup("java:comp/env/jdbc/sqlserver");
Connection conn= ds.getConnection();
System.out.println("OK");
}
catch (Exception e) {
e.printStackTrace();
}
}
Connection conn = null;
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/sqlserver");//获取连接池对象
DataSource ds = (javax.sql.DataSource) obj; //类型转换
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println(rs.getString(1) + "<BR>");
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功");
%>
java代码 失败
public static void main(String[] args) {
try {
InitialContext ict= new InitialContext();
DataSource ds =(DataSource)ict.lookup("java:comp/env/jdbc/sqlserver");
Connection conn= ds.getConnection();
System.out.println("OK");
}
catch (Exception e) {
e.printStackTrace();
}
}
#4
package com.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;
/**这是数据库连接池的公共类 **/
public class ConnectionPool {
private Vector<Connection> pool;//声明集合,里面只能是放Connection
/**
* 声明要的东西
*/
private String url = "jdbc:sqlserver://localhost:1433; database=ajax";
private String username = "sa";
private String password = "sa123";
private String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 连接池的大小,也就是连接池中有多少个数据库连接�?
*/
private int poolSize = 5;
private static ConnectionPool instance = null;
/**
* 私有的构造方法,禁止外部创建本类的对象,要想获得本类的对象,通过<code>getIstance</code>方法�?
* 使用了设计模式中的单子模式�?
*/
private ConnectionPool() {
init();
}
/**
* 连接池初始化方法,读取属性文件的内容 建立连接池中的初始连�?
*/
private void init() {
pool = new Vector<Connection>(poolSize);
//readConfig();
addConnection();
}
/**
* 返回连接到连接池�?
*/
public synchronized void release(Connection conn) {
pool.add(conn);
}
/**
* 关闭连接池中的所有数据库连接
*/
public synchronized void closePool() {
for (int i = 0; i < pool.size(); i++) {
try {
((Connection) pool.get(i)).close();
} catch (SQLException e) {
e.printStackTrace();
}
pool.remove(i);
}
}
/**
* 返回当前连接池的�?��对象
*/
public static ConnectionPool getInstance() {
if (instance == null) {
instance = new ConnectionPool();
}
return instance;
}
/**
* 返回连接池中的一个数据库连接
*/
public synchronized Connection getConnection() {
if (pool.size() > 0) {
Connection conn = pool.get(0);
pool.remove(conn);
return conn;
} else {
return null;
}
}
/**
* 在连接池中创建初始设置的的数据库连接
*/
private void addConnection() {
Connection conn = null;
for (int i = 0; i < poolSize; i++) {
try {
Class.forName(driverClassName);
conn = java.sql.DriverManager.getConnection(url, username,
password);
pool.add(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
#5
这么复杂啊。。。我不是来搞笑的。。。我在去多看看吧
#6
数据库连接池都没用到web.xml context.xml啊
那这个是什么http://cheneyjuu.blog.163.com/blog/static/41917640200922131911598/
那这个是什么http://cheneyjuu.blog.163.com/blog/static/41917640200922131911598/
#7
你可以在jsp或action(servlet)中获得连接,然后将连接再传入相应的Java类
#8
好像在java类里面是可以获得connection的!!
#1
这有深入的介绍
http://hihitiger.iteye.com/blog/899388
http://hihitiger.iteye.com/blog/899388
#2
你说的太模糊了
贴出的java连接数据库的代码
贴出的java连接数据库的代码
#3
JSP代码,成功!<%
Connection conn = null;
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/sqlserver");//获取连接池对象
DataSource ds = (javax.sql.DataSource) obj; //类型转换
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println(rs.getString(1) + "<BR>");
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功");
%>
java代码 失败
public static void main(String[] args) {
try {
InitialContext ict= new InitialContext();
DataSource ds =(DataSource)ict.lookup("java:comp/env/jdbc/sqlserver");
Connection conn= ds.getConnection();
System.out.println("OK");
}
catch (Exception e) {
e.printStackTrace();
}
}
Connection conn = null;
Context initCtx = new InitialContext();
if (initCtx == null)
throw new Exception("不能获取Context!");
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("jdbc/sqlserver");//获取连接池对象
DataSource ds = (javax.sql.DataSource) obj; //类型转换
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
out.println(rs.getString(1) + "<BR>");
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功");
%>
java代码 失败
public static void main(String[] args) {
try {
InitialContext ict= new InitialContext();
DataSource ds =(DataSource)ict.lookup("java:comp/env/jdbc/sqlserver");
Connection conn= ds.getConnection();
System.out.println("OK");
}
catch (Exception e) {
e.printStackTrace();
}
}
#4
package com.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;
/**这是数据库连接池的公共类 **/
public class ConnectionPool {
private Vector<Connection> pool;//声明集合,里面只能是放Connection
/**
* 声明要的东西
*/
private String url = "jdbc:sqlserver://localhost:1433; database=ajax";
private String username = "sa";
private String password = "sa123";
private String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
/**
* 连接池的大小,也就是连接池中有多少个数据库连接�?
*/
private int poolSize = 5;
private static ConnectionPool instance = null;
/**
* 私有的构造方法,禁止外部创建本类的对象,要想获得本类的对象,通过<code>getIstance</code>方法�?
* 使用了设计模式中的单子模式�?
*/
private ConnectionPool() {
init();
}
/**
* 连接池初始化方法,读取属性文件的内容 建立连接池中的初始连�?
*/
private void init() {
pool = new Vector<Connection>(poolSize);
//readConfig();
addConnection();
}
/**
* 返回连接到连接池�?
*/
public synchronized void release(Connection conn) {
pool.add(conn);
}
/**
* 关闭连接池中的所有数据库连接
*/
public synchronized void closePool() {
for (int i = 0; i < pool.size(); i++) {
try {
((Connection) pool.get(i)).close();
} catch (SQLException e) {
e.printStackTrace();
}
pool.remove(i);
}
}
/**
* 返回当前连接池的�?��对象
*/
public static ConnectionPool getInstance() {
if (instance == null) {
instance = new ConnectionPool();
}
return instance;
}
/**
* 返回连接池中的一个数据库连接
*/
public synchronized Connection getConnection() {
if (pool.size() > 0) {
Connection conn = pool.get(0);
pool.remove(conn);
return conn;
} else {
return null;
}
}
/**
* 在连接池中创建初始设置的的数据库连接
*/
private void addConnection() {
Connection conn = null;
for (int i = 0; i < poolSize; i++) {
try {
Class.forName(driverClassName);
conn = java.sql.DriverManager.getConnection(url, username,
password);
pool.add(conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
#5
这么复杂啊。。。我不是来搞笑的。。。我在去多看看吧
#6
数据库连接池都没用到web.xml context.xml啊
那这个是什么http://cheneyjuu.blog.163.com/blog/static/41917640200922131911598/
那这个是什么http://cheneyjuu.blog.163.com/blog/static/41917640200922131911598/
#7
你可以在jsp或action(servlet)中获得连接,然后将连接再传入相应的Java类
#8
好像在java类里面是可以获得connection的!!