enter image description here
I have this Exception but the Jar file are in the referenced libraries.
I dont't know where is the problem. The code is alright and I have added all in the build path.
String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
final String DBMS = "jdbc:mysql";
final String SERVER="localhost";
final String DATABASE = "mapDB";
final int PORT=3306;
final String USER_ID = "MapUser";
final String PASSWORD = "map";
Connection conn;//gestisce una connessione
private void initConnection() throws DatabaseConnectionException {
try {
Class.forName(DRIVER_CLASS_NAME);/
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn=(Connection)DriverManager.getConnection(DBMS + "://" + SERVER + ":" + PORT + "/" + DATABASE,USER_ID,PASSWORD);
}catch(SQLException ex) {
throw new DatabaseConnectionException();
}
}
Your image shows you have mysql-connector-java-5.1.7-bin.jar on your classpath.
When looking for that file in the Maven Repository, there are many 5.1.x versions, but not 5.1.7.
It would seem that 5.1.7 is flawed and have been retracted.
Try using another 5.1.x version, e.g. the latest, which currently is 5.1.46.
It appears that there are different versions of MySQL JDBC connectors, as the second print shows.
Maybe the existence of 2 references to the same lib is causing the ClassNotFoundException, or your project's build is not up to date.
I suggest you to keep only one version of the connector on the references of your project, clear and build the project again.
Have you tried removing mysql-connector-java-5.1.7-bin.jar, just keep mysql-connector-java-8.0.11.jar
Related
I have run into multiple problems during JBoss 5 to 7.1.1 migration. One of them is Tapestry not working at all.
The only "helpful" sign of this bug is that server responds with 404 whenewer I try to access my web app (WAR bundled within EAR) and displays "Not Found" in browser.
Even after turning every LOG4J output to DEBUG, no useful info was provided by server logs at all.
I tried upgrading different dependencies, changing war structure to comply with Tapestry specifications etc etc.
I have noticed that my application used ClasspathURLConverter accordingly to the one located here: https://wiki.apache.org/tapestry/HowToRunTapestry5OnJBoss5
However the converter is not working properly on JBoss 7
The solution to this problem was indeed in bad URL translation for Tapestry when it's looking for pages, components etc. (I had to dig really deep into the tapestry source code and debug it all the way down).
So I tried to look deeper in the VFS system and URL transformation. I have found 5+ links with the same code for converter to use (eg. http://www.voidcn.com/article/p-mpuwwlxm-eh.html). The problem with this implementation was if my JAR was located directly in {myEar}/lib/ folder. I modified the code but resulted in the path pointing to exploded but empty jar in file system.
Then I have found another solution here: https://developer.jboss.org/thread/172599
- much simpler and working.
So here is the final solution:
AppModule.java:
public static void contributeServiceOverride(MappedConfiguration<Class, Object> configuration) {
configuration.add(ClasspathURLConverter.class, new MyClasspathURLConverterImpl());
}
MyClasspathURLConverterImpl.java:
public URL convert(URL url) {
if (url != null && url.getProtocol().startsWith("vfs")) {
try {
return getRealFilePath(url.getPath());
} catch (Exception e) {
log.error(e.getCause());
}
}
return url;
}
private URL getRealFilePath(String urlString) throws IOException {
VirtualFile vFile = VFS.getChild(urlString);
URL physicalUrl = VFSUtils.getPhysicalURI(vFile).toURL();
String physicalUrlStr = physicalUrl.toString();
if (physicalUrlStr.contains(".jar")) {
int jarIdx = physicalUrlStr.indexOf(".jar");
String part1 = physicalUrlStr.substring(0, jarIdx + 4);
String part2 = physicalUrlStr.substring(jarIdx + 4);
String jarName = part1.substring(part1.lastIndexOf("/") + 1, jarIdx + 4);
String dir = part1 + part2.substring(0, part2.indexOf("/"));
String jarLocation = dir + "/" + jarName;
String packageName = part2.substring(part2.indexOf("/"));
if (packageName.startsWith("/contents")) {
packageName = packageName.substring(9);
}
String result = "jar:" + jarLocation + "!" + packageName;
physicalUrl = new URL(result);
}
return physicalUrl;
}
pom.xml
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-vfs</artifactId>
<version>3.2.14.Final</version>
<scope>provided</scope>
</dependency>
I'm asking because ALL examples I find in Google, are the same from the Fitnesse tutorial: a very simple query to a list or array in memory, NOT A REAL Database.
Yes, Fixtures never have to deal with that, but how am I supposed to test my fixtures if I can't even make the connection to the DB in a simulation of an "API"?
What I'm trying to simulate is the call from a FitNesse Fixture to query in Java into a PostgreSQL database/table. In this simple example I'm trying to obtain, at least one column from one row, in one table. When I execute the code, it runs perfectly by it's own. The problem is when trying to execute from Fitnesse through the fixture. It always fails with a ClassNotFoundException, when calling the JDBC driver. This doesn't happen by running the code by it's own.
Here is the code that does the query:
package queriespackage;
import java.sql.*;
public class testQuery01 {
public static Connection openDBConnection(){
Connection connectionString = null;
try {
String dbhost = "SOMEURL";//Redacted
String port = "SOMEPORT";//Redacted
String dbname = "THEDBNAME";//Redacted
String username = "SOMEUSER";//Redacted
String password = "SOMEPASSWORD";//Redacted
String driverJDBC = "org.postgresql.Driver";
Class.forName(driverJDBC);
connectionString = DriverManager.getConnection("jdbc:postgresql://" + dbhost + ":" + port + "/" + dbname,username,password);
connectionString.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace(System.err);
System.exit(0);
} catch (Exception e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
};
return connectionString;
};
public static ResultSet executeQuery(Connection connectionString, int intAccountId) throws SQLException{
Statement querySession = connectionString.createStatement();
//The query string
String queryString = "SELECT DISTINCT "
+ "account_search.account_id,"
+ "account_search.account_name"
+ " FROM account_search "
+ " WHERE"
+ " account_search.account_id = "+ intAccountId
+ "LIMIT 1";
ResultSet queryResult = querySession.executeQuery(queryString);
return queryResult;
};
public static String processQueryResult(ResultSet queryResult) throws SQLException{
String strQueryValueReturned = null;
while (queryResult.next()) {
strQueryValueReturned = queryResult.getString("account_name");
};
return strQueryValueReturned;
};
public static boolean closeDBConnection(Connection connectionString){
try {
if(connectionString!=null){
connectionString.close();
}
} catch (SQLException e) {
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
System.exit(0);
};
return true;
};
public static String testQuery(int intAccountId) throws SQLException, ClassNotFoundException{
boolean bolConnectionStatus = false;
String strValueReturned = null;
Connection connectionString = openDBConnection();
if(connectionString != null){
ResultSet qryQueryResult = executeQuery(connectionString, intAccountId);
strValueReturned = processQueryResult(qryQueryResult);
bolConnectionStatus = closeDBConnection(connectionString);
if(!bolConnectionStatus){
System.exit(0);
}
}else{
System.exit(0);
};
return strValueReturned;
};
};
If I add a Main method to that code, passing it the argument value for "intAccountId", it successfully returns the name of the account "account_name", just as expected.
Now here's the Fixture that should be called by the FitNesse test:
package fixturespackage;
import java.sql.SQLException;
import queriespackage.testQuery01;
public class testFixture01{
private int Int_AccountId;
//Fixture Constructor (setter)
public testFixture01(int Int_AccountId){
this.Int_AccountId = Int_AccountId;
};
public String query() throws SQLException, ClassNotFoundException{
return testQuery01.testQuery(Int_AccountId);
};
};
Just as the FitNesse guide says, there must be a "query" method, that does the actual call to the interface in the DB. I had to add a constructor instead of the "setter", because FitNesse actually demands it: "Could not invoke constructor for fixturepackage.testFixture01"
Here's the FitNesse page:
!***> System Variables
!define TEST_SYSTEM {slim}
!path C:\FitnessTest\bin
*!
|Query: fixturespackage.testFixture01|21 |
|Str_AccountName |
|SomeName |
Here's a Screenshot of my BuildPath, so you can see I have the JDBC Library for Java 8, JDK 1.8, JRE 1.8... and the "org.postgresql.Driver.class" is included in the project.
This is the error I receive, when running from FitNesse:
This is the error I get, when debugging the line where FitNesse failed by using Inspect tool:
... and YES, I also tried by hard coding the name of the JDBC:
I have searched a lot for a REAL LIFE example, both here, the FitNesse Guide and Google.
The FitNesse Guide might be extensive, but let's be sincere, it's full of "dirty word here", unrealistic and incomplete examples and missing a lot of information.
So, asking again, has anyone done a REAL LIFE test making queries, using FitNesse, that could help me find out what am I doing wrong?
I have to admit I've only done limited database tests with FitNesse, but I have used them (to query DB2).
I did not use query tables (or wrote my own fixtures to query), but instead used jdbcslim in combination with script tables and scenario's.
That fact that the driver class cannot be found suggests that although the jar is present on the classpath in your IDE it is not available when FitNesse is running your fixture code.
I notice you specify the classpath as a single directory in the wiki. In Java that means that all class files should be in that directory (as .class files, in the right subdirectory for their defined package). It will not pick up any jars (or zips) in that directory. Did you unpack your database driver's jar to that directory? If not, you need to add a !path line pointing to the jar (so the entire path including the filename) with the database driver.
Now listing every jar you need can quickly become cumbersome, so you can also use wildcards. I tend to copy all the jars I need to a single directory, that also contains my fixture .class files, and add a single !path line loading all jars in that directory.
So if you also copied your database driver to the directory in you question you could ensure it, and your own fixture, to be available via
!path C:\FitnessTest\bin
!path C:\FitnessTest\bin\*.jar
I'm using the tutorial at http://www.vogella.com/tutorials/MySQLJava/article.html
to try tp connect to my sql server on my server
When it executes the line:
Connection connect = DriverManager
.getConnection("jdbc:mysql:http://www.findmeontheweb.biz"
+ "user=findmeon_bitcoin&password=PASSWORD");
an exception gets thrown saying "No sutabled driver found for jdbc:mysql:http://www.findmeontheweb.biz
This is what I did
1. Downloaded the "mysql-connecter-java-5.1.33.bin.jar into my lib folder
2. added the jar to my project from preferences.
project code:
public class cStart {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public static void main (String[] args) {
int g=0;
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
// EXCEPTION GOES OF HEAR
Connection connect = DriverManager
.getConnection("jdbc:mysql:http://www.findmeontheweb.biz"
+ "user=findmeon_bitcoin&password=PASSWORD");
} catch (Exception e) {
System.out.println("Exception...." );
}
}
}
The URL format should be look like this
jdbc:mysql://hostname/ databaseName
I think this is a much cleaner way to do it:
String URL = "jdbc:URL_TO_YOUR_DATBASE";
String USER = "username";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS);
As seen here: http://www.tutorialspoint.com/jdbc/jdbc-db-connections.htm
I say give that link a try with your driver. You also should make sure you have the actual jar for MySQL. It really might be invalid.
I would try the one here: http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm
And then add that to your project.
The URL to the database might be wrong.
If yes you should specify a correct one with including database name.
Else verify if the jdbc driver jar is added in the build-path, if yes try to put it in the lib folder of your webapp.
I am trying to connect to mssql server from java but I couldnt figure it out. It throws an exception
ERROR: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MLS_J
here is the code below. What am I doing wrong? How can I fix this?
public static String connection_test(){
String address = "jdbc:sqlserver://192.168.1.101:1433;DatabaseName=MLS_J";
String user = "sa";
String password = "xxxx";
try {
Connection conn = DriverManager.getConnection(address, user, password);
java.sql.Statement stmt = conn.createStatement();
return "Bağlantı kapalımı? - " + conn.isClosed();
} catch (Exception e) {
return "ERROR: " + e.getMessage();
}
}
You might wanna take a look at this.
Try adding this line to your code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Follow the steps for adding jar files in build path
Right click on project
click build path-> configure build path
click libraries folder
then click add external jar and give the path of the sqljdbc4.jar.
I would suggest you to use jtds driver.
Hey stupid question but I'm having a hard time connecting my java program to a mysql database.
Throwing an exception when I hit this line.
Class.forName(driverName).newInstance();
The driver name is com.mysql.jdbc.Driver. I've searched around a bit on google and found something about a mysql-connector.jar file that I'm apparently supposed to have but I really haven't looked into it yet. Thanks.
Entire code:
Connection connection = null;
try
{
String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
Class.forName(driverName).newInstance();
String serverName = "*********";
String database = "canteen_web3";
String url = "jdbc:mysql://" + serverName + "/" + database;
final String username = "*****";
final String password = "******";
connection = DriverManager.getConnection(url,username,password);
System.out.println("Connected!");
}
catch(Exception ex)
{
throw new ICException(ex.getMessage());
}
Start your app with
java -classpath .:mysql-connector.jar MyClass
The colon separates two paths. The . is the directory you are in (and hopefully the class or the base package), the latter jar is the driver.
For further information refer to the various sources of documentation http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html