LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-14-2004, 12:47 PM   #1
linux_ub
Member
 
Registered: May 2004
Location: NY
Distribution: fedora core 1
Posts: 65

Rep: Reputation: 17
java + connection to mysql


I am trying to connect to a mysql database ... it is on the local machine ... i have downloaded the jar file and am using it while compiling.
i am using the Eclipse IDK and it does not let me compile saying "forName(String) not defined on line 16

Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbConnect {

public static void main(String[] args) {
		
	Connection con = null;
	String str = new String();
	String ssn = new String();

	try {
	  Class.forName("com.mysql.jdbc.Driver").newInstance(); //***line 16
	  con = DriverManager.getConnection("jdbc:mysql:///records");

	  if(!con.isClosed())
		System.out.println("Successfully connected to MySQL server...");
		
	  Statement stmt = con.createStatement() ;
	  str = "use records";
	  stmt.execute(str);
	  
	  ssn = "000-00-0017";
	  str = "select ssn from ssn where sn = 4"; 
	  ResultSet rs = stmt.executeQuery(str);
	  while ( rs.next() ) {
			System.out.println(rs.getString("ssn"));
		 }

	  //stmt.executeUpdate("INSERT INTO ssn " +
	//		"set ssn = \"000-00-0017\"" );	  

	} catch(Exception e) {
	  System.err.println("Exception: " + e.getMessage());
	} finally {
	  try {
		if(con != null)
		  con.close();
	  } catch(SQLException e) {}
	  }
	}
}

this is the stacktrace

java.lang.Error: Unresolved compilation problem:
The method forName(String) is undefined for the type Class

at DbConnect.main(DbConnect.java:16)
Exception in thread "main"

Last edited by linux_ub; 06-14-2004 at 01:13 PM.
 
Old 06-14-2004, 01:09 PM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
At a glance i can't really see anything wrong in your code.
Have you tried compiling it outside of Eclipse?
 
Old 06-14-2004, 10:27 PM   #3
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 31
Compiles fine for me after removing your line 16 comment... you should never combine /* without a */ .

Code:
eric.r.turner@turing:~/lab/forname$ ls
DbConnect.java
eric.r.turner@turing:~/lab/forname$ javac DbConnect.java
eric.r.turner@turing:~/lab/forname$ ls
DbConnect.class  DbConnect.java
I'm using Java version 1.4.2_01 on Linux.
 
Old 06-15-2004, 07:28 AM   #4
linux_ub
Member
 
Registered: May 2004
Location: NY
Distribution: fedora core 1
Posts: 65

Original Poster
Rep: Reputation: 17
i just put the comment for directing the readers of this post.
its wierd ... the code was not compiling yesterday .... and it is working perfectly fine today .... i havent changed one bit of it
anyway thanks to coolman0stress and eric.r.turner for their help
thanks again

Last edited by linux_ub; 06-17-2004 at 09:38 AM.
 
Old 03-04-2012, 10:26 AM   #5
AmitG
LQ Newbie
 
Registered: Mar 2012
Posts: 2

Rep: Reputation: Disabled
java+mysql in eclipse in fedora 16

can you tell me how we can connect java+mysql in eclipse in fedora 16?
 
Old 03-04-2012, 10:59 AM   #6
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 31
Quote:
Originally Posted by AmitG View Post
can you tell me how we can connect java+mysql in eclipse in fedora 16?
You need to post a new question instead of adding on to this one.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql and mysql java connector, connection refused arubin Slackware 3 03-29-2008 01:41 AM
java.lang.InstantiationException: java.sql.Connection poeta_boy Programming 2 07-06-2005 08:26 PM
[ JSP/JAVA ] MySQL connection fails White R4bbit Programming 4 04-27-2005 08:31 AM
MySQL/Java connection problem hudy Programming 3 06-23-2004 08:40 PM
Mysql Error: Lost Connection to Mysql during query ramnath Programming 5 11-18-2003 12:27 PM


All times are GMT -5. The time now is 04:42 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration