NetBeans Forums

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

Netbeans application connecting to SQL failure

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



Joined: 07 Jun 2012
Posts: 1

PostPosted: Thu Jun 07, 2012 10:14 am    Post subject: Netbeans application connecting to SQL failure Reply with quote

guys, im very new to java and using databases in netbeans and sql, so im here for help. this is my code to connect my sql database with my java application. and every time i call method connect(), it fails(displaying Offline in remark 1).

i think my driver and DB URL also my username and password are all correct. and aso i connect the databes using Services -> Databases - > MySQL Server at localhost:3306[root], but this method still fails.

anyway, i use wamp, and i use phpmyadmin

i do appreciate all the help guys. pardon me for asking something that is basic. .thanks Smile

Code:
 public void connect()
    {
        try{
        String driver = "com.mysql.jdbc.Driver";
        Class.forName(driver);
       
        String db = "jdbc:mysql://localhost:3306/database1";
        String u = "root";
        String p = "";
        con = DriverManager.getConnection(db,u,p);
        st = con.createStatement();
       
   
        remarks1.setText("Online");
        remarks1.setForeground(Color.green);
        }
        catch(Exception ex)
        {
        remarks1.setText("Offline");
        remarks1.setForeground(Color.red);
        }
    }
Back to top
Rick Fincher
Posted via mailing list.





PostPosted: Thu Jun 07, 2012 11:26 pm    Post subject: Netbeans application connecting to SQL failure Reply with quote

Hi David,

One of the cool things about NetBeans is the code help in the editor.
Inside your catch clause below type "ex" that's the error object that
was thrown. Now type a period to get "ex." After a second a list of
all the methods of the Exception Object pop up. If you don't see it
type "control-space" and it will pop up.

One of the methods you'll see is "getMessage()" it returns a string of
the error message. The "printStackTrace()" method will give you the
full trace of the exception.

Instead of "Exception" in your catch clause you an use "SQLException".

Then when you type "ex." you'll see a few more methods with SQL specific
stuff.

Usually it's best to use the most restrictive exception class rather
than the catch all "Exception", no pun intended! It's easier to isolate
the exception that way.

Also only put the code in the try clause that will generate the errors,
in this case:

con = DriverManager.getConnection(db,u,p);
st = con.createStatement();

because they can both throw SQLException.

If you can't figure it out from there, show us the error messages and we might be able to help.

Rick



On 6/7/2012 6:14 AM, starter05 wrote:
Quote:
guys, im very new to java and using databases in netbeans and sql, so im here for help. this is my code to connect my sql database with my java application. and every time i call method connect(), it fails(displaying Offline in remark 1).

i think my driver and DB URL also my username and password are all correct. and aso i connect the databes using Services -> Databases -> MySQL Server at localhost:3306[root], but this method still fails.

anyway, i use wamp, and i use phpmyadmin

i do appreciate all the help guys. pardon me for asking something that is basic. .thanks :)


Code:
public void connect()
{
try{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);

String db = "jdbc:mysql://localhost:3306/database1";
String u = "root";
String p = "";
con = DriverManager.getConnection(db,u,p);
st = con.createStatement();


remarks1.setText("Online");
remarks1.setForeground(Color.green);
}
catch(Exception ex)
{
remarks1.setText("Offline");
remarks1.setForeground(Color.red);
}
}




Back to top
Gregg Wonderly
Posted via mailing list.





PostPosted: Fri Jun 08, 2012 2:29 pm    Post subject: Netbeans application connecting to SQL failure Reply with quote

The best choice you can make about Exception processing in Java, is to start
using logging right away. Don't get into the ex.printStackTrace() path at all.

To make the use of logging easy, there are a few code completion templates
already defined in netbeans. These are visible under the Tools->Options dialog
on the Editor pane, "Code Templates" tab.

Try typing 'loge' in netbeans, in a Java file and hit return. At the top of
your class, type 'logr' and hit return. Fix imports if you need to, and then
you've now got logging. The important thing to understand about
java.util.logging, is that it is customizable and configurable from a text file.
Read the javadocs for the Logger class, for more information on that.

Gregg Wonderly

On 6/7/2012 6:25 PM, Rick Fincher wrote:
Quote:
Hi David,

One of the cool things about NetBeans is the code help in the editor. Inside
your catch clause below type "ex" that's the error object that was thrown. Now
type a period to get "ex." After a second a list of all the methods of the
Exception Object pop up. If you don't see it type "control-space" and it will
pop up.

One of the methods you'll see is "getMessage()" it returns a string of the error
message. The "printStackTrace()" method will give you the full trace of the
exception.

Instead of "Exception" in your catch clause you an use "SQLException".

Then when you type "ex." you'll see a few more methods with SQL specific stuff.

Usually it's best to use the most restrictive exception class rather than the
catch all "Exception", no pun intended! It's easier to isolate the exception
that way.

Also only put the code in the try clause that will generate the errors, in this
case:

con = DriverManager.getConnection(db,u,p);
st = con.createStatement();

because they can both throw SQLException.

If you can't figure it out from there, show us the error messages and we might
be able to help.

Rick



On 6/7/2012 6:14 AM, starter05 wrote:
Quote:
guys, im very new to java and using databases in netbeans and sql, so im here
for help. this is my code to connect my sql database with my java application.
and every time i call method connect(), it fails(displaying Offline in remark 1).

i think my driver and DB URL also my username and password are all correct.
and aso i connect the databes using Services -> Databases -> MySQL Server at
localhost:3306[root], but this method still fails.

anyway, i use wamp, and i use phpmyadmin

i do appreciate all the help guys. pardon me for asking something that is
basic. .thanks :)


Code:
public void connect()
{
try{
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);

String db = "jdbc:mysql://localhost:3306/database1";
String u = "root";
String p = "";
con = DriverManager.getConnection(db,u,p);
st = con.createStatement();


remarks1.setText("Online");
remarks1.setForeground(Color.green);
}
catch(Exception ex)
{
remarks1.setText("Offline");
remarks1.setForeground(Color.red);
}
}







Back to top
netero



Joined: 25 May 2009
Posts: 207
Location: Switzerland

PostPosted: Sun Jun 10, 2012 10:29 am    Post subject: Reply with quote

Hello,

I tried your sample in my config, and changed only the catch clause as suggested by Rick. For me your code is OK. So now I have 2 questions:

(1) Can you show us your imports? For instance, did you use these:
Code:
import java.sql.Connection;
import java.sql.DriverManager;

(2) How did you link the driver interface to an implementation?

I mean, in the project properties, there should be the path to a driver.
In my config I linked the project to
Code:
[here the NetBeans user directory]/modules/ext/mysql-connector-java-5.1.13-bin.jar
(which is the implementation delivered with NetBeans 7.0.1 for Linux). What library did you use?

Have a nice day
netero
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