PDA

View Full Version : Set a classpath for mysql and java connectivity


swethak
08-04-2008, 07:22 AM
hi,

when i run a java program for to store data and retrive using mysql datatabse i got the following errors.I think in that one of error is due to set the class path.I placed my mysql-connector-java-3.0.11-stable-bin jar in lib directory.What is the command to set a classpath in windows for java mysql connectivity.plz tell that .And also how i avoid these errors.


import java.sql.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
class Test
{
public static void main(String args[])
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//dbCon = DriverManager.getConnection("jdbc:mysql://194.230.33.43/databasename", "phoneliv_root", "root");
dbCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataMiningdb","root","root");
Connection dbCon;
ResultSet rsGroups;
Statement stmtSql;
String strSql="", strToMobileNo = "", strMemberName = "", groupId = "";

/*
** The Statement enables the resultset obtained from it to be scrollable and read only.
*/
stmtSql = dbCon.createStatement(ResultSet.TYPE_SCROLL_INSENS ITIVE, ResultSet.CONCUR_READ_ONLY);
/*
** The List of the groups are retrieved and displayed.
*/
stmtSql = dbCon.createStatement(ResultSet.TYPE_SCROLL_INSENS ITIVE, ResultSet.CONCUR_READ_ONLY);
strSql = "Select * from vehicles";
rsGroups = stmtSql.executeQuery(strSql);
while(rsGroups.next()){
strMemberName = rsGroups.getInt("vehicle_id");
strToMobileNo = rsGroups.getString("vehicle_stockno");
groupId = rsGroups.getString("vehicle_make");
}
System.out.println("strToMobileNo");
System.out.println("strMemberName");
System.out.println("groupId");
}
}


errors :

C:\j2sdk1.4.0\bin>javac dbcon.java
dbcon.java:15: cannot resolve symbol
symbol : variable dbCon
location: class Test
dbCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataMiningdb","root
","root");
^
dbcon.java:32: incompatible types
found : int
required: java.lang.String
strMemberName = rsGroups.getInt("vehicle_id");
^
2 errors

plz help me.

ess
08-04-2008, 08:59 AM
see this howto to configure java installation on windows
http://www.cs.ucsb.edu/~teliot/Path_and_Classpath.htm

and this guide (at the bottom) for mysql etc
http://eil.stanford.edu/neesgrid/data_report/src/README.txt

cheers
~E

shyam
08-16-2008, 07:05 PM
when i run a java program for to store data and retrive using mysql datatabse i got the following errors.I think in that one of error is due to set the class path.I placed my mysql-connector-java-3.0.11-stable-bin jar in lib directory.What is the command to set a classpath in windows for java mysql connectivity.plz tell that .And also how i avoid these errors.
from your example you haven't actually run ur program...u have just compiled it. heres how you set the classpath in windows...
set CLASSPATH=%CLASSPATH%;c:\my-app\lib\driver.jar;
or you could also add an entry in the My Computer->properties->advanced->environment variables



import java.sql.*;
import java.util.*;
import java.net.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
class Test
{
public static void main(String args[])
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//dbCon = DriverManager.getConnection("jdbc:mysql://194.230.33.43/databasename", "phoneliv_root", "root");
dbCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataMiningdb","root","root");
Connection dbCon;
ResultSet rsGroups;
Statement stmtSql;
String strSql="", strToMobileNo = "", strMemberName = "", groupId = "";

/*
** The Statement enables the resultset obtained from it to be scrollable and read only.
*/
stmtSql = dbCon.createStatement(ResultSet.TYPE_SCROLL_INSENS ITIVE, ResultSet.CONCUR_READ_ONLY);
/*
** The List of the groups are retrieved and displayed.
*/
stmtSql = dbCon.createStatement(ResultSet.TYPE_SCROLL_INSENS ITIVE, ResultSet.CONCUR_READ_ONLY);
strSql = "Select * from vehicles";
rsGroups = stmtSql.executeQuery(strSql);
while(rsGroups.next()){
strMemberName = rsGroups.getInt("vehicle_id");
strToMobileNo = rsGroups.getString("vehicle_stockno");
groupId = rsGroups.getString("vehicle_make");
}
System.out.println("strToMobileNo");
System.out.println("strMemberName");
System.out.println("groupId");
}
}



C:\j2sdk1.4.0\bin>javac dbcon.java
dbcon.java:15: cannot resolve symbol
symbol : variable dbCon
location: class Test
dbCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataMiningdb","root
","root");
^
you are using dbConn before even declaring it...see above the code highlighted in red

dbcon.java:32: incompatible types
found : int
required: java.lang.String
strMemberName = rsGroups.getInt("vehicle_id");
^
2 errors
here again you are trying to assign an int to a string variable...

if you had just read the errors thrown by the compiler you could have solved these problems, please find a good java tutorial and learn