Guys! Need help realizing why my code isnt working! package cwsdemoo; import jav
ID: 3701419 • Letter: G
Question
Guys! Need help realizing why my code isnt working!
package cwsdemoo;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import com.opencsv.CSVReader;
/**
*
* @author camil
*/
public class CWSDEMOO {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
writetoCSVfile();
}
public static void writetoCSVfile() {
Connection connection = null;
Statement stmt = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to a selected database...");
connection = DriverManager.getConnection("jdbc:odbc:CWHDemo", "", "");
System.out.println(" database connected successfully...");
stmt = connection.createStatement();
String sql = "SELECT * FROM fall2014";
ResultSet rs = stmt.executeQuery(sql);
String fileheader = "credit.subject,course,section,days,time";
String filename = "example.csv";
FileWriter fw = new FileWriter(filename);
fw.append(fileheader.toString());
while (rs.next()) {
int credit = rs.getInt("crn");
String subject = rs.getString("subject");
String course = rs.getString("course");
String section = rs.getString("section");
Date date = rs.getDate("date");
Time time = rs.getTime("time");
// Writing data to CSV file
fw.append(String.valueOf(credit));
fw.append(subject);
fw.append(course);
fw.append(section);
fw.write(date.toString());
fw.write(time.toString());
}
HSSFWorkbook wb = new HSSFWorkbook();
CreationHelper help = wb.getCreationHelper();
HSSFSheet sheet1 = wb.createSheet("SHEET");
CSVReader reader = new CSVReader(new FileReader(filename));
String[] line;
int i = 0;
while ((line = reader.readNext()) != null) {
Row r = sheet1.createRow(i++);
for (int j = 0; j < line.length; j++) {
r.createCell(j).setCellValue(help.createRichTextString(line[i]));
}
}
FileOutputStream out = new FileOutputStream("new.xls");
wb.write(out);
out.close();
rs.close();
} catch (SQLException se) {
Logger.getLogger(CWSDEMOO.class.getName()).log(Level.SEVERE, null, se);
} catch (ClassNotFoundException ex) {
Logger.getLogger(CWSDEMOO.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e) {
Logger.getLogger(CWSDEMOO.class.getName()).log(Level.SEVERE, null, e);
}
finally {
try {
if (stmt != null)
connection.close();
} catch (SQLException se) {
System.out.println(se);
} try {
if (connection != null)
connection.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
public static void display() {
Connection connection = null;
Statement stmt = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connecting to a selected database...");
connection = DriverManager.getConnection("jdbc:odbc:CWHDemo", "", "");
System.out.println(" database connected successfully...");
stmt = connection.createStatement();
// Query for the question What 4 credit courses are available on
// Tuesday and Thursday?
String credit = "SELECT course FROM fall2014 where credit=4 and days='tuesday' or days='thursday'";
ResultSet rs = stmt.executeQuery(credit);
System.out.println("course");
while (rs.next()) {
String course = rs.getString("course");
System.out.println(course);
}
// Query for the question What sections of OA courses are being
// offered online?
String query = "SELECT section FROM fall2014 where course='OA'";
rs = stmt.executeQuery(query);
System.out.println("section");
while (rs.next())
{
String section = rs.getString("section");
System.out.println();
}
rs.close();
} catch (SQLException se) {
System.out.println(se);
} catch (ClassNotFoundException e) {
System.out.println(e);
} finally {
try
{
if (stmt != null)
connection.close();
} catch (SQLException se) {
System.out.println(se);
} try {
if (connection != null)
connection.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
Even though there isn't any errors in the code, This is what I get when I run it:
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at cwsdemoo.CWSDEMOO.writetoCSVfile(CWSDEMOO.java:66)
at cwsdemoo.CWSDEMOO.main(CWSDEMOO.java:54)
Explanation / Answer
Download JdbcOdbc jar(licenced jar) and add that jar to the class path or place the jar in ../jre/lib folder or ../jre/lib/ext folder
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.