howto: connection pooling with mysql

P

PC Leung

The following coding always run into SQLException.

what's wrong with my coding?


try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/erptest?user=xxx&password=yyy");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users
(usernm, passwd) VALUES (?, ?)");
stmt.setString(1, sUsername);
stmt.setString(2, sPassword);
iCount = stmt.executeUpdate();
if (iCount==1) bReturn = true;
logger.info("count:" + iCount);
stmt.close();
conn.close();
} catch (SQLException E) {
 
B

Bryce

The following coding always run into SQLException.

what's wrong with my coding?

Obviously something, but its impossible to tell since you didn't give
us the entire SQLException you got. You see SQLException is a generic
exception that says something went wrong. There is another message
that goes with it that tells you more details, and that part you left
off.

It can be many things:

1. Couldn't connect to database
2. Username & password weren't right.
3. users table doesn't exist.
4. usernm column doesn't exist or accept strings.
5. passwd column doesn't exist or accept strings.
6. some unknown database error occurred, like allocation file space,
etc...

You see, there are a lot of things that could have happened...
 
R

Roedy Green

} catch (SQLException E) {

to give yourself some clues, how about:

catch ( SQLException e )
{
e.printStackTrace();
System.err.println( e.getMessage() );

}
 
B

Bryce

I suspect this is the reason - xxx and yyy doesn't look right for a
username/pasword ;-)))

If they are, then they should change their password policy... :-}
 
Q

Quinten Miller

With going into you code the best way to find out how to fix the
problem would be to use a debugger in combination with the information
held in the stack trace.
 
P

PC Leung

I have checked many times about table name, username and password.
After catching SQLException, I use logger.info()
to print a message "SQL Exception".

I guess the serlvet cannot connect to database.
I have put the connector jar file to common/lib directory.
What else should be done?
 
J

Job Numbers

PC Leung said:
I have checked many times about table name, username and password.
After catching SQLException, I use logger.info()
to print a message "SQL Exception".

I guess the serlvet cannot connect to database.
I have put the connector jar file to common/lib directory.
What else should be done?

the database might not accept tcp/ip sockets
you might have a firewall problem

any number of things. the code is probably fine. Your database setup
probably isn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,034
Messages
2,570,356
Members
47,002
Latest member
RobertoLip

Latest Threads

Top