NetBeans Forums

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

Code to connect to database not working

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



Joined: 04 Jan 2011
Posts: 64

PostPosted: Tue Apr 12, 2011 2:20 pm    Post subject: Code to connect to database not working Reply with quote

I have the following routine that I am trying to use to connect to my database:

Code:
public static void connectToDatabase() {

        String host = "jdbc:derby://localhost:1527/Test1";
        String uName = "APP";
        String uPass = "APP";

        try {
            Connection con = DriverManager.getConnection(host, uName, uPass);
        } catch (Exception e) {
            Logger.getLogger(EUtil.class.getName()).log(Level.SEVERE, null, e);
        }

    }


But, I still have to go to "Services" and connect the database manually when I start NB. How can I make this fully automatic?

Thanks
Eric
Back to top
markwade



Joined: 04 Dec 2010
Posts: 140

PostPosted: Wed Apr 13, 2011 3:34 am    Post subject: Code to connect to database not working Reply with quote

On Apr 12, 2011, at 10:20 AM, mn1247 wrote:

Quote:
I have the following routine that I am trying to use to connect to
my database:


Code:
public static void connectToDatabase() {

String host = "jdbc:derby://localhost:1527/Test1";
String uName = "APP";
String uPass = "APP";

try {
Connection con = DriverManager.getConnection(host, uName,
uPass);
} catch (Exception e) {
Logger.getLogger(EUtil.class.getName()).log(Level.SEVERE,
null, e);
}

}



But, I still have to go to "Services" and connect the database
manually when I start NB. How can I make this fully automatic?

Thanks
Eric


You aren't loading the driver:

Class.forName("org.apache.derby.jdbc.ClientDriver");
or
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

-- Mark
Back to top
jupok3



Joined: 13 Apr 2011
Posts: 1

PostPosted: Wed Apr 13, 2011 8:16 am    Post subject: Reply with quote

forum very interesting
Back to top
mn1247



Joined: 04 Jan 2011
Posts: 64

PostPosted: Wed Apr 13, 2011 1:32 pm    Post subject: Reply with quote

Thanks for the reply.

I tried

Code:
Class.forName("org.apache.derby.jdbc.ClientDriver");


But it errors with

Code:
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.


Where am I going wrong?

Eric
Back to top
markwade



Joined: 04 Dec 2010
Posts: 140

PostPosted: Wed Apr 13, 2011 4:38 pm    Post subject: Reply with quote

mn1247 wrote:
Thanks for the reply.

I tried

Code:
Class.forName("org.apache.derby.jdbc.ClientDriver");


But it errors with

Code:
java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.


Where am I going wrong?

Eric


You have to start derby.

Run startNetworkServer (there is a Windows .bat version) in the derby installation directory/bin.

In NetBeans use the services tab. If you have the project setup to use glassfish, netbeans should start derby for you.

-- Mark
Back to top
javydreamercsw



Joined: 22 Jun 2009
Posts: 456

PostPosted: Thu Apr 14, 2011 5:10 am    Post subject: Code to connect to database not working Reply with quote

Sounds like the database is not turned on. NetBeans starts a server with all the databases in it. You need to use an embedded database.

This might help:http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javadb/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans 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