Reputation: 1
I'm working on a school project and I have to connect to a database done in SQL anywhere 17 and I keep getting the flowing error:
No suitable driver found for jdbc:sqlanywhere:Tds:localhost:2638?eng="DBNAME".
Any help is welcome.
Here is the code for the class doing the connection.
package model;
import java.sql.*;
public class DBConnection {
public class ConnectionHandler {
ResultSet r;
Statement s;
String returnStatus;
}
private String db, user, passwd;
private String dbUrl;
private Connection conn;
public DBConnection(String db, String user, String passwd, Object openserver, String databasepath) {
this.db = db;
this.user = user;
this.passwd = passwd;
dbUrl = "jdbc:sqlanywhere:Tds:localhost:2638?eng=" + db;
try {
conn = DriverManager.getConnection(dbUrl, user, passwd);
conn.setAutoCommit(false);
} catch (Exception e) {
System.out.println(e);
Boolean openServer = (Boolean) openserver;
if (openServer.booleanValue())
try {
String engcommand = "dbeng12 " + databasepath + db + ".db";
Runtime.getRuntime().exec(engcommand);
long t0, t1;
t0 = System.currentTimeMillis();
do {
t1 = System.currentTimeMillis();
} while (t1 - t0 < 5000);
conn = DriverManager.getConnection(dbUrl, user, passwd);
conn.setAutoCommit(false);
} catch (Exception ex) {
System.out.println("Unable to start server. " + ex);
}
}
}
public void updateStatement(String sql, ConnectionHandler ch) {
Integer result = new Integer(0);
String message = null;
if (sql.trim().substring(0, 1).equalsIgnoreCase("U"))
message = "Number of rows updated: ";
else if (sql.trim().substring(0, 1).equalsIgnoreCase("I"))
message = "Number of rows inserted: ";
else
message = " ";
try {
ch.s = conn.createStatement();
result = new Integer(ch.s.executeUpdate(sql));
ch.returnStatus = message + result;
} catch (Exception e) {
System.out.println("Unable to execute the insert/update/delete statement. " + e);
ch.returnStatus = "Error:" + e.toString();
}
}
public void returnResultSetSelectStatement(String sql, int resultsetType, ConnectionHandler ch) {
ch.r = null;
try {
if (resultsetType == 1)
ch.s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
else
ch.s = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ch.r = ch.s.executeQuery(sql);
} catch (Exception e) {
ch.returnStatus = e.toString();
System.out.println("Unable to execute the returnSelectStatement. " + e);
}
}
}
Upvotes: 0
Views: 3878
Reputation: 11
Use "jdbc:sqlanywhere:DSN=SQL Anywhere 17 Demo;Password=sql"
http://dcx.sap.com/sqla170/en/html/3bd5b17c6c5f1014945fd2b7ec7cba6a.html
Upvotes: 0
Reputation: 4135
You need to add sajdbc4.jar
to the classpath of your program. If you are using IDE such as NetBeans or Eclipse, you can add sajdbc4.jar
as a dependent library and NetBeans/Eclipse will automatically add it to classpath.
If you are running the above program from command line, copy sajdbc4.jar
to the folder where the above Java program is located and then compile the file using the following command (this adds sajdbc4.jar
to classpath),
javac -classpath ./sajdbc4.jar DbConnection.java
Run the Java program using the following command (sajdbc4.jar is added to classpath),
java -classpath "./sajdbc4.jar;." DbConnection
Alternatively you can also add the Maven dependency, if you are using Maven to build your project.
Upvotes: 1