我有一个名为“conpool”的MS SQLServer 2008数据库。并在那里创建了一个id,fullName和age的表。 现在我已经在Netbeans 7.3和Tomcat 7.0.30中使用Java实现了一个连接池,每次与数据库建立连接时都不会绑定,我可以为我的数据库创建查询。
我的连接池:
// ------------------------ Beginn JDBC Connection Pool ----------------- -----------------
public static void main(String[] args) throws SQLException, NamingException {
// Get DataSource
Context initialContext = new InitialContext();
System.out.println("Test1: a object from InitialContext has been created");
Context context = (Context) initialContext.lookup("java:/comp/env");
System.out.println("Test2: Context lookup was OKAY");
//The JDBC Data source that was created in SQLServer
DataSource datasource = (DataSource) context.lookup("jdbc/conpool");
System.out.println("Test3: DataSource lookup with JNDI was successfully");
System.out.println("JDBC Connecction Pool is created successfully");
//-------------------------Ende JDBC Connection Pool--------------------------------------
//Using a connection from the pool
Connection connection = datasource.getConnection();
System.out.println("Test4: Datasource Connection was successfully");
if (connection == null) {
throw new SQLException("Error establishing connection!");
//System.out.println("DBConnection Failed!!! ");
}
//Using the connection to access the database
//--------------------------------Beginn der Queryprocess------------------------------------------
String query = "SELECT * FROM conpool.dbo.Personen";
PreparedStatement statement = connection.prepareStatement(query);
ResultSet rs = statement.executeQuery();
System.out.println("Test5: All Object for a single Query are created");
while (rs.next()) {
System.out.println(rs.getString(query));
}
//Connection will be closed
connection.close();
//-------------------------------Ende der Queryprocess--------------------------------------
}
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>Tomcat-ConnectionPooling with MS SQLServer</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/conpool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
我的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/ITWService">
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/conpool" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="dbo" password="" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=conpool"/>
</Context>
现在我遇到了这种错误:
运行:
线程“main”中的异常javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者在applet参数中或在应用程序资源文件中指定类名:java.naming.factory.initial 在javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) 在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 在javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) 在javax.naming.InitialContext.lookup(InitialContext.java:392) 在com.akapoor.itwservice.server.RestServer.main(RestServer.java:136) Java结果:1 建立成功(总时间:1秒)
请帮助我解决我的问题。我非常感谢你,我很困惑。我尝试了一切,但没有帮助..请告诉我,我的问题在哪里,为什么我没有连接到我的数据库
答案 0 :(得分:0)
您需要在托管环境(如应用程序服务器)中创建一个不带参数的InitialContext。
由于您发布的代码有一个主要方法,我假设您已将代码作为独立应用程序执行。但是在你的问题中你写的是你的应用程序在Tomcat上运行,所以你需要将它部署到你的容器并在那里调用它。
或者您是否有理由想远程访问JNDI?