How to include driver class and/or class path to connect to a SQL Server 2000 database in Jdeveloper? - classpath

I want to connect to a database in Jdeveloper 11g but there seems to be a problem with my driver for SQL Server 2000. I have downloaded the appropriate driver (Sqljdbc4.jar) but I do not know how to include or use it in my code.
import java.sql.*;
class TestConnection
{
public static void main (String[] args)
{
try
{
// Step 1: Load the JDBC ODBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Step 2: Establish the connection to the database
String url = "jdbc:sqlserver://10.1.73.180\\SQL2000;" +
"databaseName=reportgen;user=sa;password=*****;";
Connection conn = DriverManager.getConnection(url);
System.out.println("Connected.");
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
and this shows when I try to run my TestConnection.java class:
Got an exception!
com.microsoft.sqlserver.jdbc.SQLServerDriver
Process exited with exit code 0.

Project properties->libraries and class path->add JAR.
You might also need to add this to the embedded WebLogic instance.
You can also use tools->manage libraries to add a library and then add that library to your project.

Related

Get the PostgreSQL server version from connection?

Is there anything in the modern PostgreSQL connection protocol that would indicate the server version?
And if not, is there a special low-level request that an endpoint can execute against an open connection to pull the server details that would contain the version?
I'm looking at a possible extension of node-postgres that would automatically provide the server version upon every fresh connection. And I want to know if this is at all possible.
Having to execute SELECT version() upon every new connection and then parsing it is too high-level for the base driver that manages the connection. It should be done on the protocol level.
After a bit of research, I found that PostgreSQL does provide server version during connection, within the start-up message.
And specifically within node-postgres driver, we can make Pool provide a custom Client that handles event parameterStatus on the connection, and exposes the server version:
const {Client, Pool} = require('pg');
class MyClient extends Client {
constructor(config) {
super(config);
this.connection.on('parameterStatus', msg => {
if (msg.parameterName === 'server_version') {
this.version = msg.parameterValue;
}
});
}
}
const cn = {
database: 'my-db',
user: 'postgres',
password: 'bla-bla',
Client: MyClient // here's our custom Client type
};
const pool = new Pool(cn);
pool.connect()
.then(client => {
console.log('Server Version:', client.version);
client.release(true);
})
.catch(console.error);
On my test PC, I use PostgreSQL v11.2, so this test outputs:
Server Version: 11.2
UPDATE - 1
Library pg-promise has been updated to support the same functionality in TypeScript. And you can find a complete example in this ticket.
UPDATE - 2
See example here:
// tests connection and returns Postgres server version,
// if successful; or else rejects with connection error:
async function testConnection() {
const c = await db.connect(); // try to connect
c.done(); // success, release connection
return c.client.serverVersion; // return server version
}

java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase

I have mariadb 10.4 running on my system. The db connection is good, i have tested with db workbench with the root password.
On the other hand, I try to connect to it from my Java code, i have the exception below:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost:3306/mydatabase
My java code is as below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class CountryDAO {
public void list() throws SQLException{
String databaseURL = "jdbc:mariadb://localhost:3306/mydatabase";
String user = "root";
String password = "root";
try
{
Connection connection = DriverManager.getConnection(databaseURL, user, password);
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
}
}
I am calling this code from a servlet.
I am using eclipse and i have added the mariadb-java-client-2.4.2.jar as external jar from build path. It should be working but it doesn't.
UPDATE: Firstly, I have tested my code in a new Eclipse Java project and it works like a charm. Secondly, i have tried to call the same method from a main method of another class, the db connection also work perfectly. So i guess my problem is because i call this connection from a Servlet. I don't know why it does so but i am kind of stuck. Do you have any suggestions?
You have to load the driver first.
Class.forName("org.mariadb.jdbc.Driver");

PostgreSQL database Versioning using Liquibase

I'm trying to use Liquibase version 3.6 to maintain database version changes. I'm able to execute database version changes when i need to execute single file changes.
I have using below code to execute version changes successfully, but my intention is to dynamically execute the change logs from a folder and not single file. I'm using only Java for all configuration of Liquibase
#Startup
#Singleton
#TransactionManagement(TransactionManagementType.BEAN)
public class InitializerBean {
#Resource(mappedName = "java:/M2M_RESOURCES")
private DataSource ds;
#PostConstruct
protected void bootstrap() {
ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor(getClass().getClassLoader());
try (Connection connection = ds.getConnection()) {
JdbcConnection jdbcConnection = new JdbcConnection(connection);
Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);
Liquibase liquiBase = new Liquibase("data/liquibase/", resourceAccessor, db);
liquiBase.update("Development");
} catch (SQLException | LiquibaseException e) {
}
}
}
When the first parameter of Liquibase class is single file , liquibase is able to execute changes but when I intent to execute all file of single folder is not able to track and execute changes.
I'm using JDK 1.8 and file in data/liquibase/ is dbChangelog.sql and dbChangelog_2.sq. This all code is deployed on Wildfly 10 as part of ear archive

Loading SQL script in Vertx

I have been trying to load the SQL script schema into MySQL DB using Vertx.
Though, i am able to load or update any single DB command but unable to load complete schema in one go.
The second challenge faced is that, this might be blocking code for Vertx application. If that is the case, how can it be avoided?
Here is the code snippet i have been trying to execute:
jdbcClient.getConnection(resConn -> {
if(resConn.succeeded()) {
SQLConnection connection = resConn.result();
connection.execute("<Trying to load the SQL Script schema>", resSchema -> {
connection.close();
if(resSchema.succeeded()) {
async.complete();
} else {
testContext.fail("Failed to load bootstrap schema: " + resSchema.cause().getMessage());
}
});
} else {
testContext.fail("Failed to obtain DB connection for schema write");
}
});
The MySQL JDBC driver does not allow to execute a file as a SQL script. You must parse the script and execute individual commands one by one.

GWT JDBC LDAP connection fails

I am trying to connect my GWT application to an ldap server using jdbc, but could not make it work so far.
Here is a code sample of my attempt to connect to it:
String ldapConnectString = "jdbc:ldap://SERVERIP:389/dc=SERVERNAME,dc=office,dc=COMPANY,dc=com?SEARCH_SCOPE:=subTreeScope";
java.sql.Connection con;
try {
con = DriverManager.getConnection(ldapConnectString,"cn=USERNAME","PASSWORD");
} catch (SQLException e) {
System.out.println("An error has ocurred!!! Connection failed");
e.printStackTrace();
}
The example I used to write this is: http://myvd.sourceforge.net/bridge.html
When I run the application I get following error message:
java.sql.SQLException: No suitable driver found for jdbc:ldap://SERVERIP:389/dc=SERVERNAME,dc=office,dc=COMPANY,dc=com?SEARCH_SCOPE:=subTreeScope
I would be thankful for any help
Edit:
The code sample I provided is running on server side accessed by RPC. I included 2 jar files in my lib/ directory downloaded from here: http://sourceforge.net/projects/myvd/files/jdbc%20ldap%20bridge/jdbc%20ldap%20bridge%202.1/jdbc-ldap-2.1.zip/download
You generally need to register the JDBC driver before you can connect to the backend.
Try something like
DriverManager.registerDriver(new com.octetstring.jdbcLdap.sql.JdbcLdapDriver());
before setting up the connection.
More general information on ways of registering drivers.