[jboss-user] [JNDI/Naming/Network] - Exception javax.naming.NoInitalContextException
cffranco
do-not-reply at jboss.com
Mon May 25 10:35:22 EDT 2009
Hi,
I need to insert some data in my database(hsqldb), but is not working. I am using the connection via a datasource, and by doing jndi lookup. When you run the code, I receive the exception:
--------------------------------------------------------------------------------
Exception in thread "main" 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
---------------------------------------------------------------------------------
Please somebody help me.? ;o)
My code:
package org.jboss.soa.esb.dbaccess;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class InsertRecords {
/** String driver = "org.hsqldb.jdbcDriver";
String url = "jdbc:hsqldb:hsqldb\\localDB";
String username = "sa";
String password = "";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;**/
public static void main(String[] args) throws Exception {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/FtpZip");
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = ds.getConnection();
String query = "insert into teste(name_file) values('lala')";
pstmt = conn.prepareStatement(query); // create a statement
pstmt.setInt(1, 1); // set input parameter 1
//pstmt.setString(2, "deptname"); // set input parameter 2
//pstmt.setString(3, "deptLocation"); // set input parameter 3
pstmt.executeUpdate(); // execute insert statement
} catch (Exception e) {
e.printStackTrace();
} finally {
pstmt.close();
conn.close();
}
}
}
Thanks..
[]´s
Claudemir
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4233065#4233065
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4233065
More information about the jboss-user
mailing list