Reputation: 772
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();
}
Upvotes: 1
Views: 268
Reputation: 5003
The function DbConnection() returns null because you don't return any value. Neither do you create any Connection instance. If you're creating the DataSource manually, you won't need to bind it to jndi.
If you're creating a DataSource, get the Connection from the DataSource object (via its getConnection() method)
Upvotes: 1