NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
learningNB
Joined: 11 May 2009 Posts: 5
|
Posted: Tue May 19, 2009 7:14 am Post subject: [solved]how to connect to database without storing userid and password in code |
|
|
Hi all, this is my code below: | Code: | Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/myJDBC");
[b]Connection con = ds.getConnection("tony","tonydb")[/b];
stmt = con.createStatement();
result = stmt.executeQuery("SELECT * FROM student");
result.close();
stmt.close();
con.close(); | And I can get a connection to the db. If I were to amend the bold line to | Code: | | Connection con = ds.getConnection(); | I will get the error below
| Code: | | java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Access denied for user ''@'localhost' (using password: NO) | I think storing the userid and password in the code is not wise. How can i not store the userid and password in the code and still get the db connection? I have already created a JDBC resource in netbeans, why can't I get connected without specifying the userid/password in the code?
web.xml | Code: | <resource-ref>
<res-ref-name>jdbc/myJDBC</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref> |
sun-resource.xml | Code: | <resources>
<jdbc-connection-pool
name="myJDBC"
datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
res-type="javax.sql.DataSource">
<property name="tony" value="DB_USER"/>
<property name="tonydb" value="DB_PASSWORD"/>
<property name="url" value="jdbc:mysql://localhost:3306/weblogic"/>
</jdbc-connection-pool>
<jdbc-resource
enabled="true"
jndi-name="jdbc/myJDBC"
object-type="user"
pool-name="myJDBC"/>
</resources> |
Please advise how I can remove the userid and password from my code.
Last edited by learningNB on Tue May 19, 2009 9:51 am; edited 1 time in total |
|
| Back to top |
|
 |
learningNB
Joined: 11 May 2009 Posts: 5
|
Posted: Tue May 19, 2009 9:51 am Post subject: |
|
|
Hi all, problem solved. I did not specify my login userid and password in the jbbc configuration at Glassfish. Once I did that, I could connect with | Code: | | Connection con = ds.getConnection(); |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|