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");
}
}
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");
}
}