Java Eclipse JDBC ClassNotFoundException - java

I'm trying to connect to an external database and no matter what I do I keep getting the following error:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at DbCon.main(DbCon.java:10)
My code to set up the driver:
import java.sql.*;
public class DbCon {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
}
}
I've tried omitting the driver entirely, and just trying to connect with:
String connectionUrl = "jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;databaseName=xxxx;user=xxxx;password=xxxx;";
try {
Connection con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
e.printStackTrace();
}
This only gets me the following error:
java.sql.SQLException: No suitable driver found for xxxx
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DbCon.main(DbCon.java:15)
I've tried adding the jar to Eclipse's classpath, I've added the folder to the project's build path as an external class folder.
Is there something I'm missing?

Related

STEP Files Parsing

I am working to parse STEP files ISO-10303-21; using Part21Parser in java
Following is file structure
HEADER;
FILE_DESCRIPTION(('STEP AP214'),'1');
FILE_NAME('s25552_b159_a1_13.stp','2016-10-04T07:52:56',(' '),(' '),'Spatial InterOp 3D',' ',' ');
FILE_SCHEMA(('automotive_design'));
ENDSEC;
static Model read_file (String filename) throws STDevException, IOException {
Part21Parser parser = new Part21Parser();
return parser.parse(filename);
}
public static void main(String[] args) {
try {
Part21Parser parser = new Part21Parser();
try {
Model m1 = read_file("D:/Braunschweig_board.stp");
} catch (IOException e) {
e.printStackTrace();
}
} catch (STDevException e1) {
e1.printStackTrace();
}
But when I run, it gives exception that
Cannot find schema automotive_design
I am not sure what kind of schema is it expecting and what should be the valid schema as there is no help regarding this exception online. Any one who worked on STEP/EXPRESS files could please help
Following is stacktrace
com.steptools.stdev.SchemaNotFoundException: Cannot find schema automotive_design
at com.steptools.stdev.SchemaBase.forName(SchemaBase.java:61)
at com.steptools.stdev.p21.Part21Parser.getSchema(Part21Parser.java:693)
at com.steptools.stdev.p21.Part21Parser.getSchema(Part21Parser.java:673)
at com.steptools.stdev.p21.Part21Parser.getPopulation(Part21Parser.java:547)
at com.steptools.stdev.p21.Part21Parser.parse_DATA_SECTION(Part21Parser.java:561)
at com.steptools.stdev.p21.Part21Parser.parse_EXCHANGE_FILE(Part21Parser.java:302)
at com.steptools.stdev.p21.Part21Parser.parse(Part21Parser.java:230)
at com.steptools.stdev.p21.Part21Parser.parse(Part21Parser.java:112)
at com.steptools.stdev.p21.Part21Parser.parse(Part21Parser.java:92)
at com.steptools.stdev.p21.Part21Parser.parse(Part21Parser.java:80)
at CadParser.read_file(CadParser.java:26)
at CadParser.main(CadParser.java:35)
Caused by: java.lang.ClassNotFoundException: com.steptools.schemas.automotive_design.Schema
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)`enter code here`
at java.lang.Class.forName(Unknown Source)
at com.steptools.stdev.SchemaBase.forName(SchemaBase.java:59)
... 11 more

Java MongoDb Connector for JasperReports

Does anyone have a example about how to make a java mongoDB connector to use with JasperReports? I have been trying make it work but without any sucess... All example on internet looks like old versions of JasperReports...
I tried this code
GeneratePDF with JasperReports Library and MongoDB
String mongoURI = "mongodb://bdsandbox6:27017/test";
MongoDbConnection connection = null;
Map<String, Object> parameters = new HashMap<String, Object>();
try {
connection = new MongoDbConnection(mongoURI, null, null);
parameters.put(MongoDbDataSource.CONNECTION, connection);
File jasperFile;
jasperFile = new File("MongoDbReport.jasper");
JasperCompileManager.compileReportToFile("MongoDbReport.jrxml", "MongoDbReport.jasper");
JasperFillManager.fillReportToFile("MongoDbReport.jasper", parameters);
JasperExportManager.exportReportToPdfFile("MongoDbReport.jrprint");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
But I ways got this error:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/log4j/Logger at
com.jaspersoft.mongodb.connection.MongoDbConnection.(MongoDbConnection.java:64)
at serverTeste.Rest.main(Rest.java:22) Caused by:
java.lang.ClassNotFoundException: org.apache.log4j.Logger at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more
I know it seens I need log4j.jar but i tried it and I faill again. I guess I am using the wrong jars files...
I also tried it:
GeneratePDF with JasperReport Library and MongoDB
Without Sucess

No class found LoggerFactory

I'm wanting to read an iCalendar (ics file) for a project and thinking it might be easier to just use an API. After much research I found iCal4j.
I'm new to ical4j, and have been searching for an answer, and trying to do research and found that the creator of it allows any form of logging they want, so I'm really baffled. I'm trying to get just a simple example I found working. I'm horrible with different APIs, and it kills me. I just have the iCal4j-2.0-beta1.jar in my classpath, and I have the following code:
import java.io.*;
import net.fortuna.ical4j.data.*;
import net.fortuna.ical4j.model.*;
public class iCalendarSupport
{
public static void main(String[] args)
{
try
{
FileInputStream fin = new FileInputStream("C:\\Users\\Mike\\Downloads\\basic (1).ics");
CalendarBuilder builder = new CalendarBuilder();
Calendar calendar = builder.build(fin);
System.out.println(calendar.getComponent("Event").toString());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here's the output I have in the console in eclipse.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at net.fortuna.ical4j.util.Configurator.<clinit>(Configurator.java:51)
at net.fortuna.ical4j.data.CalendarParserFactory.<clinit>(CalendarParserFactory.java:63)
at net.fortuna.ical4j.data.CalendarBuilder.<init>(CalendarBuilder.java:104)
at iCalendarSupport.main(iCalendarSupport.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
Any help would be much appreciated.
Classpath is missing the SLF4J (Simple Logging Facade) library. You can find it here:
http://www.slf4j.org/download.html

HSQL Connection Error. Any insight?

I'm trying to connect to HSQL using java. I'm using a tutorial to work with JDBC and hibernate. It's the lynda tutorials. Anyway, here's the code below:
package com.lynda.javatraining.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
private static final String USERNAME = "dbuser";
private static final String PASSWORD = "dbpassword";
private static final String CONN_STRING =
"jdbc:hsqldb://data/explorecalifornia";
public static void main(String[] args) throws SQLException {
//Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
System.out.println("A");
System.out.println(conn == null);
System.out.println(CONN_STRING);
try {
System.out.println("B1");
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("B1-2");
System.out.println("Connected!");
} catch (SQLException e) {
System.out.println("B2");
System.err.println(e);
} finally {
System.out.println("B3");
if (conn != null) {
conn.close();
}
}
System.out.println("C");
}
}
Here's the error I'm getting:
A
true
jdbc:hsqldb://data/explorecalifornia
B1
2014-06-27T15:26:27.430-0400 SEVERE could not reopen database
org.hsqldb.HsqlException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile#76d682a[file =/data/explorecalifornia.lck, exists=false, locked=false, valid=false, ] method: openRAF reason: java.io.FileNotFoundException: /data/explorecalifornia.lck (No such file or directory)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.persist.LockFile.newLockFileLock(Unknown Source)
at org.hsqldb.persist.Logger.acquireLock(Unknown Source)
at org.hsqldb.persist.Logger.openPersistence(Unknown Source)
at org.hsqldb.Database.reopen(Unknown Source)
at org.hsqldb.Database.open(Unknown Source)
at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
at org.hsqldb.DatabaseManager.newSession(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at com.lynda.javatraining.db.Main.main(Main.java:24)
B2
java.sql.SQLException: Database lock acquisition failure: lockFile: org.hsqldb.persist.LockFile#76d682a[file =/data/explorecalifornia.lck, exists=false, locked=false, valid=false, ] method: openRAF reason: java.io.FileNotFoundException: /data/explorecalifornia.lck (No such file or directory)
B3
C
Can anyone tell e what I"m doing wrong? Thanks.
A database has a lock file explorecalifornia.lck that prevents communication. You should delete lock file and restart the database. This may happens from time to time when you accidentally shutdown the database or system.
See the syntax of the command used from the command line to invoke shutdown. There's also an option how to shutdown server in Java.

Java SQL Exception when creating a DSNless connection

Here is my code:
package dsnless;
import java.sql.*;
////////////////////////////////////////////////////////////
//This class will be used to fire queries to the database
////////////////////////////////////////////////////////////
public class Query {
public Query(){
String pathToDatabase = "E:/Eclipse Projects/JDBC/src/datasouce/School.mdb";
String database = "jdbc:odbc:Driver="+
"{Microsoft Access Driver(*.mdb,*.accdb)};" +
":DBQ=" + pathToDatabase;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(database);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new Query();
}
Connection con;
Statement s;
ResultSet r;
}//class Query end
Here is the exception:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] DRIVER keyword syntax error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dsnless.Query.<init>(Query.java:14)
at dsnless.Query.main(Query.java:20)
And there is a new exception, too:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at dsnless.Query.<init>(Query.java:14)
at dsnless.Query.main(Query.java:20)
Question
Please tell me what is wrong. I suspect it is the String database but stil it is only a guess.
Nailed it, myself
I changed the extensions supported from (*.mdb,*.accdb) to only (*.mdb) and it worked. Any idea as to why that happened???
You have an additional colon here, remove it
String database = "jdbc:odbc:Driver="+
"{Microsoft Access Driver(*.mdb,*.accdb)};" +
":DBQ=" + pathToDatabase;
^

Categories