首页上一页 1 下一页尾页 3 条记录 1/1页
jsp项目开发全程实录 第七章 数据源异常
发表在JavaWeb图书答疑
2009-05-19
是否精华
是
否
版块置顶:
是
否
程序是:
package org.fql.JDBCconnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class JDBConnecton {
private static Connection conn = null;
private static DataSource ds = null;
private ResultSet rs = null;
private Statement st = null;
static {
try {
Context ctx = new InitialContext();
ctx = (Context) ctx.lookup("java:comp/env");//这是第二十三行
ds = (DataSource) ctx.lookup("TestJNDI");
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet excuteQuery(String sql) {
try {
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
System.out.println("query异常!");
}
return rs;
}
public boolean excuteUpdate(String sql) {
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Update异常!");
return false;
}
}
public static void main(String[] args)
{
System.out.println(JDBConnecton.conn);
}
}
异常是:
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
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.fql.JDBCconnection.JDBConnecton.<clinit>(JDBConnecton.java:23)
null
package org.fql.JDBCconnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class JDBConnecton {
private static Connection conn = null;
private static DataSource ds = null;
private ResultSet rs = null;
private Statement st = null;
static {
try {
Context ctx = new InitialContext();
ctx = (Context) ctx.lookup("java:comp/env");//这是第二十三行
ds = (DataSource) ctx.lookup("TestJNDI");
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet excuteQuery(String sql) {
try {
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = st.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
System.out.println("query异常!");
}
return rs;
}
public boolean excuteUpdate(String sql) {
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Update异常!");
return false;
}
}
public static void main(String[] args)
{
System.out.println(JDBConnecton.conn);
}
}
异常是:
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
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.fql.JDBCconnection.JDBConnecton.<clinit>(JDBConnecton.java:23)
null