This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes java with mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java with mysql" Watch "java with mysql" New topic
Author

java with mysql

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I am very new to java.I need a sample code to add any two fields to mysql database and to retrieve the values and to display in a page.


Thanks.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What have you tried? Where are you stuck?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I havent yet started to do the code.i dont know where to start.I have just tested a simple code to print.but i am getting the result as Cannot connect to database server which is in catch.

public class Connect
{
public static void main (String[] args)
{
String createString;

Statement stmt;
Connection conn = null;

try
{
String userName = "root";
String password = "1234";
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection ("jdbc:mysql://localhost/test", "root", "1234");
st = con.createStatement();
st.executeUpdate(createString);
st.close();

System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Either the database does not exist, or the password is wrong.

I have just tried your code, only changing the password and the query (into "SHOW TABLES"), and it printed "Database connection established", followed by "Database connection terminated".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This:

is not good practice. You are replacing the helpful exception message with an unhelpful message. Do this instead:

and post the stack trace.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I think the 'database' is not within Java environment and its totally a different system, it needs some 'intermediate' person to talk with. That's what they call as a 'Driver'.

Have you given a thought about it?

Beforehand, had you have a database in your machine and created the tables first of all?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Rob,
could you show me where to put the show tables
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I replaced the code with

try {
...
} catch (Exception e) {
e.printStackTrace();
} .

but i am getting the exception like this
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
Originally posted by preethi Ayyappan:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

How did you add the MySQL database driver to your classpath? It seems that it is not present.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Originally posted by preethi Ayyappan:
Rob,
could you show me where to put the show tables

I needed a query to execute, so I replaced the reference to "createString" into a query I knew would work - SHOW TABLES.

But Ulf has already found what the problem is.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's what i had told already. You need to have the "mysql-connector" jar file in your classpath for both compilation and running.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Now I had installed my sql jar files
1.jdbc2_0-stdext.jar
2.jta-spec1_0_1.jar
3.jdbc-connector.jar
4.mysql-connector.jar
5.mysql-connector-java-3.0.17-ga-bin.jar

in the path /usr/java/jdk1.6.0_03/lib/mysql-connector-java-3.0.17-ga/lib. my java file is in /usr/java/jdk1.6.0_03/bin.Is it correct?
Now also i amgetting the same error
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
pls guide me.

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41141
    
  45
It doesn't matter in which directory you put the file - it needs to be in your classpath. You might run your program like

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I have tried your code to run like this:
java -classpath .:/usr/java/jdk1.6.0_03/lib/mysql-connector-java-3.0.17-ga/lib/mysql-connector-java-3.0.17-ga-bin.jar Connect.

But i am getting the following error:

java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Now i put the jar files in correct path and i got the result.

Thanks.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's great. Congrats preethi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java with mysql
 
Similar Threads
WA #1.....word association
Advice of database type is needed
which sql going in database
getting last row
Using datasource in netbeans to get connection