"No suitable driver found" for UCanAccess connection - java

I have no idea what wrong is with my sample code, I've followed all instructions on multiple YouTube videos and other online sources. I just can't fix it. Could someone explain what is the problem and how to solve it? All the required lib's are already added.
I have made sure all the libraries are correctly installed, Yet it still does not find the driver.
This is my code
public boolean checkLogin(String username, String password) {
try {
Connection myconObj;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
URL databaseLocation = this.getClass().getResource("/com/vanstryp/database/MainUserData.accdb");
myconObj = DriverManager.getConnection("jdbc:ucanaccess:/" + databaseLocation);
ResultSet result;
Statement stmt = conn.createStatement();
String query = "select * from MainUserData";
result = stmt.executeQuery(query);
while (result.next()) {
String dbUsername = result.getString("Username");
String dbPassword = result.getString("Password");
System.out.println();
if (username.equalsIgnoreCase(dbUsername) && password.equals(dbPassword)) {
PrintWriter activeUser = new PrintWriter(new FileWriter("activeUser.db"));
activeUser.println(dbUsername);
activeUser.close();
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
This is the error message:
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess:/file:/C:/Users/Jaco%20van%20Stryp/Dropbox/Fitness%20Perfect/build/classes/com/vanstryp/database/MainUserData.accdb

java.sql.SQLException: No suitable driver found for jdbc:ucanaccess:/file:/C:/Users/Jaco%20van%20Stryp/Dropbox/Fitness%20Perfect/build/classes/com/vanstryp/database/MainUserData.accdb
A UCanAccess connection URL must begin with jdbc:ucanaccess:// followed by the path to the database file. Your connection URL begins with jdbc:ucanaccess:/ (only one slash) and it also includes the spurious file: designator. It should look more like this:
jdbc:ucanaccess://C:/path/to/database/file.accdb

Related

Unable to connect to Azure using JDBC (based upon the connection string and samples provide) due to SSL issue

Am getting the following error: com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Connection reset by peer: socket write error."
import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
public class SQLDatabaseConnection {
// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) {
String connectionString =
"jdbc:sqlserver://XXXXX.database.windows.net:1433;"
+ "database=VDB;"
+ "user=XXX#VVV;"
+ "password=XXXX;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
// Declare the JDBC objects.
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(connectionString);
// Create and execute a SELECT SQL statement.
String selectSql = "SELECT TOP 2 * from Application";
statement = connection.createStatement();
resultSet = statement.executeQuery(selectSql);
// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(2) + " "
+ resultSet.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the connections after the data has been handled.
if (resultSet != null) try {
resultSet.close();
} catch (Exception e) {
}
if (statement != null) try {
statement.close();
} catch (Exception e) {
}
if (connection != null) try {
connection.close();
} catch (Exception e) {
}
}
}
}
I'm only trying to do the "sample" connection snippet of code as referenced on the Azure site (which points to a MS entry), modified only to match my db and test table but without success.
Having reviewed all there is to know, I have:-
ensured that I'm using the right sqljdbc (I've tried all 4)
have the sqlauth.dll on the CLASSPATH
have set the sample up EXACTLY as shown; and incorporated the string that Azure offers.
I have tried various combinations of encrypt and trust without success. As I'm a newbie to Java and Azure, I'm reluctant and unsure how to fiddle with the JVM security settings.
I've proven that my machine can talk to the Azure database (through a VB ODBC connection); and I've tested with the firewall down.
Any thoughts?
I tried to reproduce the issue, but failed that I could access my SQL Azure Instance using the code which be similar with yours.
The difference between our codes is only as below, besides using the connection string of my sql azure instance.
Using the driver sqljdbc4.jar from the sqljdbc_4.0 link.
Using the code Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); to load MS SQL JDBC driver.
Not adding the sqlauth.dll file into the CLASSPATH.
Check my client IP which has been allowed by SQL Azure IP firewall.
Using the sql select 1+1 to test my code, and get the value 4 from code result.getInt(1).
That's fine for me. If you can supply more detals for us, I think it's very helpful for analysising the issue.
Hope it helps.

GWT and SQL - No Suitable Driver

I'm currently having a very strange problem with GWT and SQL.
I'm trying to get my GWT program to communicate with my SQL server. I took several examples from the web and they would all terminate with the following error:
SEVERE: No suitable driver found for jdbc:mysql://localhost:3306/table
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/table
After racking my brain for hours and hours, I finally came up with the following code which DOES work. Running the following will happily output the MySQL version to the console:
public static void printVersion() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/table";
String user = "username";
String password = "password";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
However, as soon as I changed the method slightly from outputting the string to the console to returning the string to a calling function, I started getting the "No suitable driver found" error again.
The change I made was extremely subtle; Here is the diff:
1c1
< public static String getVersion() {
---
> public static void printVersion() {
10d9
< String rtn = null;
18c17
< rtn = rs.getString(1);
---
> System.out.println(rs.getString(1));
42d40
< return rtn;
The calling function simply changed from an Version.printVersion() to String str = Version.getVersion().
I'm fairly experienced in PHP and am just making the jump to GWT/Java. I searched all over and haven't been able to find out why this isn't working like it should. Numerous tutorials and pre-existing code have failed me, so I'm asking here.
I do have the MySQL JDBC driver in my classpath, and I have tried several permutations loading it with Class.forName("com.mysql.jdbc.Driver").newInstance();, but to no avail
I'm sure I'm missing something really stupid, but I'm too inexperienced to have any clue what.
I honestly don't know why the Class.forName("com.mysql.jdbc.Driver").newInstance(); wasn't working before. I tried several methods with that and none of them worked. (I have since deleted my non-working work, so I can't compare why it didn't work) I did try it in this example just now though and it does appear to work when added. Here is the diff:
1c1
< public static String getVersion() {
---
> public static void printVersion() {
10d9
< String rtn = null;
13d11
< Class.forName("com.mysql.jdbc.Driver").newInstance();
19c17
< rtn = rs.getString(1);
---
> System.out.println(rs.getString(1));
43d40
< return rtn;
Basically just add the Class.forName("com.mysql.jdbc.Driver").newInstance(); right before you initialize the connection.
I have absolutely ZERO idea why it would work without that statement if I'm just printing the output but wouldn't work if I was returning the output. Mega bonus-points to whoever can describe that to me.
Edit:
Borked the diff - you need to add exception handlers for InstantiationException, IllegalAccessException and ClassNotFoundException as well. Your IDE should prompt you of this though.

Java - JDBC MySQL connection issue

I am trying to establish a connection but for some reason I am having issues doing so.
I think it may be a syntax error - but I am not completely sure. I have commented out numerous syntax approaches I have tried. I used MySQL documentation for help, even when following their syntax i get problems.
When I run the code without the Connection the program jumps into the try. But as soon as I add the Connection code it jumps to the catch section of the code - therefore there must be an issue with the syntax.
Can anyone spot where I have gone wrong? Thank You in advance.
public void selectData()
{
try
{
Connection con = null;
//Accessing driver from the JAR file
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Class.forName("com.mysql.jdbc");
//String a = "jdbc:mysql://localhost:3306/c3361434?profileSQL=true";
///String a = "jdbc:mysql://address=(protocol=tcp)(host=localhost)(port=3306)(user=root)(password=root)/c3361434";
//Connection con = DriverManager.getConnection("jdbc:mysql://address=(protocol=tcp)(host=localhost)(port=3306)(user=root)/c3361434");
//Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/c3361434", "root", "root");
//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/c3361434?" + "user=root&password=root");
Output3.setText("Connection has been established");
/*
PreparedStatement statement = con.prepareStatement("SELECT * FROM RSA-data");
ResultSet result = statement.executeQuery();
while(result.next())
{
Output2.setText(result.getString(1));
Output3.setText("in the while loop");
}
*/
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Output4.setText("db fail");
}
}
try it:
Check if you mysql is running, in command line type:
mysqld --install
In java source:
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB?dontTrackOpenResources=true");
the command mysqld not reconized because you need to enter at path specific.
For example:
MS - DOS:
cd C:/MySQL/bin
C:/MySQL/bin>mysqld --install
Into folder bin there is the mysqld.exe
Do you got it ?

android.os.on main thread exception

I am trying to connect to my sql DB on my sql server, using the below code.
But when I run it, the above error shows.
I have added the permissions line to the manifest, but still no luck.
Any advice will be appreciated!
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
String connString = "jdbc:jtds:sqlserver://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
String username = "xxxxxx";
String password = "xxxxxxxxxx";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from TableName");
//Print the data to the console
while(reset.next()){
Log.w("Data:",reset.getString(3));
//Log.w("Data",reset.getString(2));
}
conn.close();
} catch (Exception e) {
Log.w("Error connection","" + e.getMessage());
}
NetworkOnMainThreadException: The exception that is thrown when an application attempts to perform a networking operation on its main thread.
You should call sendfeedback method on asynctask then only above code will work. As webserver is taking lot of time to response main thread becomes unresponsive. To avoid it you should call it on another thread. Hence AsyncTask is better.
http://android-developers.blogspot.in/2009/05/painless-threading.html

MySQL DATA Truncation Error occuring from Java application but not directly on MySQL

I am using the JDBC driver to connect to a mysql database and using the "LOAD DATA INFILE" command in my java application to load(insert) a text file into the database. I am getting the following error: Data truncation: Data too long for column xxx at row 1.
However if I load the same text file manually by logging into the database and entering the SQL manually, the data loads fine.
Can someone pelase tell me what the error might be?
I am running this on Red Hat Enterprise Linux Server release 5.8 and the jdk version is 1.5.0_16 if that helps
This is the function used to load the data
public static void loaddata(Connection conn, String filename, String tablename)
{
try{
Statement stmt = null;
stmt = conn.createStatement();
File file = new File(filename);
file.getAbsolutePath().replace("\\", "\\\\");
String cmd = "LOAD DATA INFILE '"
+ file.getAbsolutePath().replace("\\", "\\\\")
+ "' INTO TABLE " + tablename + " FIELDS TERMINATED BY \'^\'";
stmt.executeUpdate(cmd);
System.out.println("cmd :" + cmd);
}
catch(SQLException sqle){
sqle.printStackTrace();
}
}
This is the function to create the JDBC connection:
public static Connection createConnection()
{
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e) {
e.printStackTrace();
}
try {
String url = ""; //URL mentioned in the actual code
conn = DriverManager.getConnection(url, user, pass);
} catch (SQLException sqe1) {
sqe1.printStackTrace();
}
return conn;
}

Categories

Resources