Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java with mysql

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried? Where are you stuck?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
could you show me where to put the show tables
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i put the jar files in correct path and i got the result.

Thanks.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great. Congrats preethi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic