NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

[solved]how to connect to database without storing userid and password in code

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
learningNB



Joined: 11 May 2009
Posts: 5

PostPosted: Tue May 19, 2009 7:14 am    Post subject: [solved]how to connect to database without storing userid and password in code Reply with quote

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

PostPosted: Tue May 19, 2009 9:51 am    Post subject: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo