1

I want to connect to a database that is created on WAMP. It is a SQL database that I created using SQL Buddy on WAMP.

When I run the code it showed

SEVERE: javax.naming.NameAlreadyBoundException: Use rebind to override

I found out I should use the asadmin command to disbale the JNDI naming on glassfish which worked once but again it showed the same error.

public Connection DbConnection(){

                        Connection con = null;
                        DataSource ds = new DataSource();
                        ds.setDatabaseName("mydatabase");
                        ds.setDescription("Authorization");
                        Context ctx = new InitialContext();
                        ctx.bind("jdbc/mydatabase", ds);
                        ds.getConnection("root", "");
                        return con;
}

public void myStatement(){

                   PreparedStatement pst;
                   Connection conn = DbConnection();   
                   conn.setAutoCommit(false);
                   pst = conn.prepareStatement("Select *" +
                                               " from Member WHERE Username = ? ");
                   pst.setString(1, username);
                   conn.commit();
                   pst.close();   
}
4

1 回答 1

1

函数 DbConnection() 返回 null 因为您不返回任何值。您也不会创建任何 Connection 实例。如果您手动创建 DataSource,则无需将其绑定到 jndi。

如果您正在创建 DataSource,请从 DataSource 对象获取 Connection(通过其 getConnection() 方法)

于 2013-02-11T04:24:49.113 回答