1
NetBean 7.0で単純なJavaプロジェクトを作成し、MS SQlサポート用のjarファイルsqljdbc4.jarを追加しました。context.bindを使用してDataSourceをバインドする方法。接続プーリングなど
MQ SQL用のデータソース(以下のコード)を作成したクラスを作成しました。 データソースを使用してデータベースを接続し、レコード数(504)を正常に取得します。 (私は504レコードを持つProductテーブルを持っています)
しかし、Contextにバインドしようとするとエラーになります。誰かが私が "???"を埋めるために何を入力すべきかを示唆するのに役立つでしょうか?下のコードでは? 「データソースへの接続」
package datasourcetest;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import java.sql.*;
import java.util.Hashtable;
/**
*
* @author admin
*/
public class dataSource {
public static void main(String [] args)
{
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("admin-PC\\SQLEXPRESS");
ds.setPortNumber(1433);
ds.setUser("sa");
ds.setPassword("admin");
try{
System.out.println("PART 1");
Connection con = ds.getConnection();
if(con !=null)
{
System.out.println("Connection Success");
}
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from AdventureWorks.Production.Product") ;
while(rs.next()) {
int count = rs.getInt(1);
System.out.println("Total Record: "+ count);
}
/* Bind the DataSource to JNDI so that we can look for the datasource by the name given**/
System.out.println("PART 2");
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY," ??? ");
env.put(Context.PROVIDER_URL, " ??? ");
Context ctx = new InitialContext(env);
ctx.bind("jdbc/myDatasource",ds);
SQLServerDataSource myDs = (SQLServerDataSource)ctx.lookup("jdbc/myDatasource");
}
catch(SQLException se) {
System.out.println("Failed PART 1");
}
catch(NamingException ne) {
System.out.println("Failed PART 2");
ne.printStackTrace();
}
}
}
おかげ - リンクは有益であるが、INITIAL_CONTEXT_FACTORYとPROVIDER_URLの値を言いません。エラーをスローする:javax.naming.NoInitialContextException:クラスをインスタンス化できません:???? [ルート例外はjava.lang.ClassNotFoundException:???? ] – Steer360
質問に対する解決策はありますか? – Lite