Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Query a Record from the Database 2

Status
Not open for further replies.

tonioJ

Programmer
Oct 7, 2002
90
PH
Hello I am new to JAVA using JDBC. But I already have and understanding how it works. I just want to ask a question what is wrong with my source code because I keep on getting an error saying Driver is not found: the source code is below:

import java.sql.*;

public Connection cnn = null;

private void btnConnectSQLActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

ConnectToSQL();
GetRecords();

}

private void GetRecords(){

try{
String mySQLStatement = "Select * from FRIENDSLIST";
Statement stmt = cnn.createStatement();
ResultSet rs=stmt.executeQuery(mySQLStatement);


//PreparedStatement stmt = cnn.prepareStatement(mySQLStatement);
//ResultSet rs.stmt.executeQuery();


if (rs.getFetchSize() != 0) {
txtLastName.setText(rs.getString("LAST_NAME"));
};




} catch(NullPointerException e){
System.out.println("NullPointerException");
}
catch(Exception e){
System.out.println("Query Failed !");
}
}

private void ConnectToSQL(){
try {
// jdbc:jtds:sqlserver://<hostname>[:<port][/<dbname>][;<property>=<value>[;...]]


String driverName = "net.sourceforge.jtds.jdbc.Driver"; // NetDirect JDBC driver
String serverName = "197.121.7.15";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber + "/JavaSample";
String url = "jdbc:jtds:sqlserver://" + mydatabase; // a JDBC url
String username = "sa";
String password = "admin";

// Load the JDBC driver
Class.forName(driverName);

// Create a connection to the database
cnn = DriverManager.getConnection(url, username, password);
}catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.println("Could not find DB Driver");
} catch (SQLException e) {
// Could not connect to the database
System.out.println("Could not connect to DB");
}


}
 
Maybe some more details about the erros should be useful, but at first sight it seems that the driver class files is not in the classpath.

Cheers.

Dian.
 
yeah you're right Dian,

how will I do it??? I mean how will I put the driver class file in the classpath?? And where can I find this classpath??
 
if you are in something like tomcat, then add the jtds driver .jar file to TOMCAT_HOME/common/lib.

If you are running the class from the command line or a console, then stype this before your run the code :

(on dos/windows)
set CLASSPATH=%CLASSPATH%;C:\where\my\jar\file\is\jar_file_name.jar

(on linux/unix)
export CLASSPATH=$CLASSPATH;/where/my/jar/file/is/jar_file_name.jar

--------------------------------------------------
Free Database Connection Pooling Software
 
guys, thanks for all your help...

i can already connect to the database but still it's returning an error... i couldn't get to query the records inorder for me to diplay them on the text fields.

i have 3 records in my table. the fetchsize of the resultset is 1 and it returns an error

java.sql.SQLException: Cursor may only be accessed in a forward direction.
 
>>> java.sql.SQLException: Cursor may only be accessed in a forward direction.

Your code is a bit incorrect - it should be more like :

Code:
ResultSet rs=stmt.executeQuery(mySQLStatement);
while (rs.next()) {
    txtLastName.setText(rs.getString("LAST_NAME"));
}

--------------------------------------------------
Free Database Connection Pooling Software
 
sedj,

thanks for your help. i was able to figure it out.

with this code:
ResultSet rs= null;
rs = stmt.executeQuery(mySQLStatement);
rs.beforeFirst();
rs.next();

at least this will be my stepping stone to database application development using java.... :)
 
Looking good :)
There's no need to initialize the rs with null first though, try this:
Code:
ResultSet rs = stmt.executeQuery(mySQLStatement);
rs.beforeFirst();
rs.next();

haslo@haslo.ch - www.haslo.ch​
 
Anyway, I think initializing variables is a good practise. That can avoid scope problems, "spurious" data in loops and so on.

Cheers.

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top