"Invalid authorization specification" error on UCanAccess connect - java

I am having a problem with the UCanAccess driver namely that when I attempt to connect to a database using the following code
public static void main(String[] args) {
//establish connection with database in order to execute sql queries
try {
conn = DriverManager.getConnection("jdbc:ucanaccess://H:\\IT_PAT_Program_LOCALONLY\\IT_Pat_Database.accdb;showschema=true");
System.out.println("Connection Established");
} catch (SQLException ex) {
System.out.println("Could Not Connect to database\n"+ex);
}
//closes the database connection at program shutdown
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
conn.close();
System.out.println("Shutdown Succesful");
} catch (SQLException ex) {
System.out.println("An exception occured\n"+ex);
}
}
});
}
I am met with the following error:
Could Not Connect to database
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 invalid authorization specification - not found: Admin
The database is also connected as a persistence unit however since I'm unaware of any way to make use of that from within the code (Google has been no help) this method seems to be my only option.

We can not see where you are doing the authorization in your code.
user ?
password ?
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection(
"jdbc:ucanaccess://<mdb or accdb file path>",user, password);

Related

I tried to connect to SQL Server 2014 but an error always occurs, how to solve it

Note: I included the mssql-jdbc-6.4.0.jre7 jar file
The error:
W/System.err: com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed". ClientConnectionId:0c66e1a1-9a3a-4554-9137-9868b305b033
the source code:
public void click (View view)
{
new Thread(new Runnable(){
#Override
public void run()
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con;
con = DriverManager.getConnection("jdbc:sqlserver://IP:port;databaseName=DB;user=user;password=password");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
It looks like server isn't available, now, check it

java web app refuses to connect to SQL Database when deployed on Tomcat

So i am facing the following problem.
I have developed an web app that has the following connection to a SQL Server database. (db connection code attached)
public class DBConnection
{
private DatabaseMetaData dma;
private static Connection con;
private static DBConnection instance = null;
private static String security = "integratedSecurity=true;";
private DBConnection()
{
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
} catch (Exception e) {
System.out.println("Can not find the driver");
System.out.println(e.getMessage());
}
try{
con = DriverManager.getConnection("jdbc:jtds:sqlserver://<Machine>;databaseName=<DBName>; useNTLMv2=true;");
//set autocommit
con.setAutoCommit(true);
dma = con.getMetaData(); // get meta data
System.out.println("Connection to " + dma.getURL());
System.out.println("Driver " + dma.getDriverName());
System.out.println("Database product name " + dma.getDatabaseProductName());
}
catch(Exception e){
System.out.println("Problems with the connection to the database");
System.out.println(e.getMessage());
System.out.println(con);
}
}
public static void closeConnection()
{
try{
con.close();
System.out.println("The connection is closed");
}
catch (Exception e){
System.out.println("Error trying to close the database " + e.getMessage());
}
}
public Connection getDBcon()
{
return con;
}
public static DBConnection getInstance()
{
if (instance == null)
{
instance = new DBConnection();
}
return instance;
}
public static void startTransaction()
{ try{
con.setAutoCommit(false);
}
catch(Exception e){
System.out.println("fail start transaction");
System.out.println(e.getMessage());
}
}
public static void commitTransaction()
{ try{
con.setAutoCommit(true);
}
catch(Exception e){
System.out.println("fail commit transaction");
System.out.println(e.getMessage());
}
}
public static void rollbackTransaction()
{
try
{
con.rollback();
con.setAutoCommit(true);
}
catch(Exception e){
System.out.println("fail rollback transaction");
System.out.println(e.getMessage());
}
}
I am using a Tomcat 8 on InteliJ IDE for running the app and debugging. Which works fine. (DB connection is established)
The problem is that when i take the war file and deploy it in the same Tomcat Server i get no DB Connection. (No DB connection)
I have checked all the .jar files in the tomcat and the project and I have added all the needed files.
Can't seem to find what is causing this issue. Maybe there is someone who got stuck with the same issue
I can't get a proper undertanding what is causing this issue and how to fix it
**EDIT: Following I have added the error displayed when trying to load data
type Exception report
message Request processing failed; nested exception is java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request
processing failed; nested exception is java.lang.NullPointerException
root cause
java.lang.NullPointerException
com.lifeletapp.business.dataLayer.DbLogIn.isValidUser(DbLogIn.java:27)
com.lifeletapp.business.HelloController.verifyLogin(HelloController.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
DbLogin class:
public class DbLogIn implements ILogIn
{
private Connection conn;
public DbLogIn()
{
conn = DBConnection.getInstance().getDBcon();
}
public Staff isValidUser(String userName, String password)
{
Staff staff = new Staff();
try {
String query = "SELECT * FROM Staff WHERE userName=? AND pass=?";
PreparedStatement preparedStatement = conn.prepareStatement( query );
preparedStatement.setString(1, userName);
preparedStatement.setString(2, password);
ResultSet resultSet = preparedStatement.executeQuery();
while( resultSet.next() ) {
staff.setUserID(resultSet.getInt("userID"));
staff.setfName(resultSet.getString("fName") );
staff.setlName(resultSet.getString("lName") );
staff.setUserName(resultSet.getString("userName") );
staff.setPass(resultSet.getString("pass") );
staff.setEmail(resultSet.getString("email") );
}
resultSet.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return staff;
}
}
This is more a config or server issue. Not a code issue.
As mentioned in the comments I have tested if var conn == null -> resulted true. And the connection dose not get nulled anywhere. Please view code. Then again the above code works when run from the InteliJ debugger.
So finally figured out what was the issue.
Why was it working on my IDE config:
1. I was using java JDK 1.7 as JAVA_HOME
2. I was using an older version of Tomcat as a build
3. In order to work with JDTS you need to extract the nlmauth.dll from the .jar archive and copy it to the configured env JDK(jdk1.7-->>bin->>copy here)
Why was it not working on Tomcat server:
1.I was using Tomcat 8.xxx
2.Tomcat 8.xx require JDk 8
3. In the JDK 8 i have not past the nlmauth.dll ( once i have done this everything was working)
In order to approach this issue the first clue will be looking into the tomcat server logs.
On my presumption this issue is prom to occur only when you have an Database connection establish via Integrated Security.
My presumption is related to the fact that in the tomcat log the main error that was denying the JDBC driver was based on the integrated security credentials.
Best of luck to you all out there.

JDBC Pool is suspended, , cannot allocate resources to applications

I deployed some apps in weblogic server. Few days ago, I traced logs and saw the error message:
2016-09-22 12:58:33,442 ERROR CommonService - ------- ERROR --------- java.sql.SQLException: Internal error: Cannot obtain XAConnection weblogic.common.resourcepool.ResourceDisabledException: Pool jdbc/*** is Suspended, cannot allocate resources to applications..
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResourceInternal(ResourcePoolImpl.java:377)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:342)
at weblogic.common.resourcepool.ResourcePoolImpl.reserveResource(ResourcePoolImpl.java:329)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:417)
at weblogic.jdbc.common.internal.ConnectionPool.reserve(ConnectionPool.java:324)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:94)
at weblogic.jdbc.common.internal.ConnectionPoolManager.reserve(ConnectionPoolManager.java:63)
at weblogic.jdbc.jta.DataSource.getXAConnectionFromPool(DataSource.java:1677)
at weblogic.jdbc.jta.DataSource.refreshXAConnAndEnlist(DataSource.java:1475)
at weblogic.jdbc.jta.DataSource.getConnection(DataSource.java:446)
at weblogic.jdbc.jta.DataSource.connect(DataSource.java:403)
at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:364)
at [my-package].ConnectionHandler.newDatabaseConnection(ConnectionHandler.java:37)
I think that having a app leeks connections and doesn't return them to the pool
At temporary solution, I have to extended the the connection pool.
I try to research which apps made this problem and see that some strange codes below:
public class ConnectionHandler
{
..
public ConnectionHandler()
{
logger.trace("ConnectionHandler() constructor called");
}
static Connection newDatabaseConnection() throws SQLException
{
Connection conn;
try {
Context initContext = new InitialContext();
DataSource dataSource = (DataSource) initContext.lookup(LOOKUP_URL);
conn = dataSource.getConnection();
conn.setAutoCommit(false);
} catch (NamingException e) {
logger.error("------- ERROR ---------", e);
throw new ProcessingError("Could not obtain database connection!");
}
return conn;
}
}
This app (SOAP service) will using the code below to query data once having requests:
if (connectionHandler == null) {
connectionHandler = new ConnectionHandler();
}
try {
conn = connectionHandler.newDatabaseConnection();
// Some callable statements here
conn.commit();
logger.info("------- OK ---------");
} catch (SQLException e) {
logger.error("------- ERROR ---------", e);
} catch (InstantiationException e) {
logger.error("------- ERROR ---------", e);
} catch (IllegalAccessException e) {
logger.error("------- ERROR ---------", e);
} catch (DossierServiceException e) {
logger.error("------- ERROR ---------", e);
} finally {
jdbc.close(conn);
}
My confusions which I do not understand yet:
Using the static connection in multi threads is ok?
Create new class (ConnectionHandler) for each request and then get static connection?
Just close the connection without closing the ResultSet, Callable statements?
Could you help me explanation for these ones or having some solutions else to prevent this problem?
The method is static not the result it's returning. static method means that it's not necessary to have an instance of the enclosing class to call it
Yes, that's a mistake, but, not such a big deal
Yes, it would be better first to close the result set, statement and the connection, but this should do the job
The problem with this code is that there's no conn.rollback() in catch blocks and there may be uncaught runtime exceptions that will not be rolled back

Hanging on DriverManager.GetConnection() in java

I have an application that requires a connection to a MS SQL Server 2008 database. Now I have the jdbc driver and I have been struggling to get it to connect successfully. This is my connection code:
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
int duration = Toast.LENGTH_SHORT;
Connection con = DriverManager.getConnection(jdbc:jtds:sqlserver://41.76.209.156:1433; databaseName=prooptin_ProOptData", "username", "password");
textview7.setText(con.toString());
textview7.setText("Successful");
} catch (ClassNotFoundException e) {
textview7.setText("Could not find the database driver " + e.getMessage());
} catch (SQLException e) {
textview7.setText("Could not connect to the database " + e.getMessage());
} catch (Exception e) {
textview7.setText(e.getMessage());
}
It seems to hang at the DriverManager.getConnection() line. I have tried to alter the connection string, and tried it with and without the username and password and database name, and nothing will work. It does not print an exception message, just returns a blank message and nothing happens.

Cant connect to specific schema in mysql

i'm trying to connect with jdbc to a specific schema in mysql server, the schema name is mining
when i'm trying to connect i get access to all the DB and therefore the executing statements apply to all the schemes in my db instead only to "mining"
this is how i establish a connection:
public class Mining {
Connection conn;
void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
this.conn = DriverManager.getConnection(
"jdbc:mysql://localhost/?currentSchema=mining","admin","admin" );
//I ALSO TRIED THIS: "jdbc:mysql://localhost/mining","admin","admin"
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
any thoughts?
thanks!
try,
private Connection connect = DriverManager
.getConnection("jdbc:mysql://localhost/mining?"
+ "user=admin&password=admin");
String databaseURL = jdbc:mysql://hostName:portNumber/schemaName?
Connection connectionObj = DriverManager.getConnection(databaseURL,userName,password);

Categories