How to host nanohttpd java app on AWS? - java

I've wrote simple hello-world http server app with nanohttpd:
package name.antonsmirnov.apptogether.service.http;
import fi.iki.elonen.NanoHTTPD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
/**
* HTTP server
*/
public class HttpSample {
private static Logger logger = LoggerFactory.getLogger(HttpSample.class);
private NanoHTTPD server;
private int port;
private String host;
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
private AtomicInteger counter = new AtomicInteger(0);
private void initHttp() {
server = new NanoHTTPD(host, port) {
#Override
public Response serve(IHTTPSession session) {
String ip = session.getHeaders().get("http-client-ip");
logger.info("Request from " + ip);
String message = "hello " + ip + " " + counter.incrementAndGet();
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, message);
}
};
}
/**
* Start HTTP server
* #throws Exception
*/
public void start() throws Exception {
if (started)
return;
initHttp();
started = true;
doStart();
}
private void doStart() throws IOException {
logger.info("Starting at port {} ...", port);
server.start();
logger.debug("Started");
}
/**
* Stop HTTP server
* #throws Exception
*/
public void stop() throws Exception {
if (!started)
return;
started = false;
doStop();
}
private void doStop() {
logger.info("Stopping ...");
server.stop();
logger.debug("Stopped");
}
private boolean started;
public boolean isStarted() {
return started;
}
public static void main(String[] args) {
HttpSample httpSample = new HttpSample();
httpSample.setPort(5555);
httpSample.setHost("0.0.0.0");
try {
httpSample.start();
} catch (Exception e) {
logger.error("Failed to start", e);
return;
}
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
logger.error("Interrupted");
}
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>name.antonsmirnov.apptogether</groupId>
<artifactId>http-sample</artifactId>
<version>1.0</version>
<description>HTTP server</description>
<dependencies>
<!-- http server -->
<dependency>
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd</artifactId>
<version>2.2.0</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>name.antonsmirnov.apptogether.service.http.HttpSample</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
after maven clean package i'm able to run it locally with java -jar ...-with-dependencies.jar and i can navigate to 127.0.0.1:5555 and see response.
When i host it on AWS Elastic Beanstalk as "web tier"
i can see it's started in logs:
-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2018/06/06 19:34:57 [error] 3230#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 5.189.7.39, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "http.us-east-2.elasticbeanstalk.com"
2018/06/06 19:34:58 [error] 3230#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 5.189.7.39, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:5000/favicon.ico", host: "http.us-east-2.elasticbeanstalk.com", referrer: "http://http.us-east-2.elasticbeanstalk.com/"
-------------------------------------
/var/log/web-1.log
-------------------------------------
19:32:58.043 [main] INFO HttpSample - Starting at port 5555 ...
19:32:58.062 [main] DEBUG HttpSample - Started
-------------------------------------
/var/log/web-1.error.log
-------------------------------------
But it's unreachable in http://http.us-east-2.elasticbeanstalk.com:5555
I can't see requests in the log (i should be able to see smth like 00:59:25.864 [NanoHttpd Request Processor (#1)] INFO HttpSample$1 - Request from x.y.z.k like on localhost). Instead i see ERR_CONNECTION_TIMED_OUT error.
What's wrong? Should i configure ports for VPC somehow? Does it conflict with nginx?

I had just allow incoming requests on port 5555 in security group:
Not sure if it conflicts with nginx if i decide to use port 80 though.

Related

camel-quarkus-amqp: Erroneous proceed messages not requeued / route not transactional

Problem:
When the further processing of a message fails, than the message is not requeued but just lost for good. Otherwise everything is working fine.
Environment:
Quarkus application on AKS with camel-quarkus-amqp, queue = Azure service bus
We have similar applications to that one, running on a jboss server with the same configuration (service bus queue with same properties, similar camel route setup) that are not showing that behaviour.
Code:
The application was previously built based on quarkus 1.11.3.Final but even after an update to 2.7.6.Final, the behaviour is the same.
from pom.xml (standard like from code generation):
<properties>
...
<quarkus.platform.version>2.7.6.Final</quarkus.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-bean</artifactId>
</dependency>
<dependencies>
application.properties:
quarkus.qpid-jms.url=failover:(amqps://xyz.servicebus.windows.net)
quarkus.qpid-jms.username=xxx
quarkus.qpid-jms.password=xxx
Implementation - Consumer:
public class MessageConsumer extends RouteBuilder {
#Inject private UpdateService updateService;
#Override
public void configure() {
from("amqp:queue:" + "queueName")
.routeId(MessageConsumer.class.getName() + ".consumeQueue")
.process(exchange -> exchange.getIn().setBody(UUID.fromString(exchange.getIn().getBody(String.class))))
.bean(updateService);
}
}
Implementation - Producer:
import javax.inject.Inject;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import java.util.UUID;
public class MessageRepository {
#Inject ConnectionFactory connectionFactory;
public void sendMessage(UUID uuid) {
try (JMSContext context = connectionFactory.createContext(JMSContext.AUTO_ACKNOWLEDGE)) {
context.createProducer().send(context.createQueue("queueName"), uuid.toString());
} catch (Exception e) {
throw new ApplicationException("Invalid message");
}
}
}
Question:
What do I need to change / add to get messages requeued (not acknowledged) if an exception will be throw inside the bean processing of updateService in the camel route?
The likely issue here is that you need to enable transactions on the route so that the consumption of the message that fails will trigger a rollback of the transaction when an error is thrown.
There is considerable documentation about this and how transaction are managed in the Camel docs here.
Since there is a lack of transactional support for AMQP in camel-quarkus in versions below 3.4.0, I needed to do that plain without camel. Here is my implementation as reference for others maybe facing the same or similar problem:
public class MessageConsumer implements Runnable {
private static final long WAIT_UNTIL_RECONNECT = 30000; // 30 seconds
#Inject private UpdateService updateService;
#Inject ConnectionFactory connectionFactory;
private final ExecutorService scheduler = Executors.newSingleThreadExecutor();
void onStart(#Observes StartupEvent ev) { scheduler.submit(this); }
void onStop(#Observes ShutdownEvent ev) { scheduler.shutdown(); }
#SneakyThrows
#Override
public void run() {
// loop here to make sure that it will reconnect to the queue in case of disconnect
while(true) {
try {
openConnectionAndConsume();
} catch (Exception e) {
log.error("Exception in connection: ", e);
Thread.sleep(WAIT_UNTIL_RECONNECT);
}
}
}
private void openConnectionAndConsume() {
try (JMSContext context = connectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
JMSConsumer consumer = context.createConsumer(context.createQueue(messagingProperties.getQueue()))) {
while (true) {
Message message = consumer.receive();
handleMessage(message);
}
}
}
private void handleMessage(Message message) {
if (message == null) return;
try {
updateService.update(UUID.fromString(message.getBody(String.class)));
message.acknowledge();
} catch (Exception e) {
log.error("Error in route: ", e);
}
}
}

Load web app (servlet on Tomcat) in a remote server

I developed a web application that runs on my computer on localhost. Then I loaded the war file into catalina home on a remote server. Web app runs but it stops when it try to connect to database on server.
The connection is a jbdc connection on localhost, the database is mysql. When I do a connection on my computer, no problems occour.
String connectionString="jdbc:mysql://192.168.0.100:3306/"+request.getSession().getAttribute("dbname");
Connection con=null;
try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.sendRedirect("Errore.html");
return;
};
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=(Connection) DriverManager.getConnection(connectionString,"root","root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The Connection con is null,DriveManager.getConnection doesn't work and I don't know why.
I also tried with postgresql connection but the problem is the same.
Must I configure something in remote server?
The Server is debian 9.2 like my computer.
I think you need to define your connection string not as a localhost but as a full IP:
String connectionString="jdbc:mysql://xx.xx.xx.xx:3306/"+request.getSession().getAttribute("dbname");
Try the following code to test the connection. Its a Maven project.
The pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.test</groupId>
<artifactId>mavenproject2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and the src/main/java/App.java (please replace the connection data)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class App {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String host = "localhost";
String db = "mysql";
String user = "root";
String password = "root";
String connectionString = String.format("jdbc:mysql://%s:3306/%s", host, db);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(connectionString, user, password);
System.out.println("Everthing looks alright!");
}
}
If you compile this project you will get a mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Upload the JAR to your remote system and try it there
java -jar mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Maven not including jar files in classpath during packaging

I'm getting started with Maven and have come across a problem I'm unable to resolve. It seems the jar files required for my applicaiton to run are not in the classpath. Should Maven not be taking care of this during mvn package?
When I run mvn package, I get error:
[ERROR] /home/dev/Desktop/maventest/my-app/src/main/java/com/mycompany/app/App.java:[79,9] cannot find symbol
symbol: class UpnpService
location: class com.mycompany.app.App
[ERROR] /home/dev/Desktop/maventest/my-app/src/main/java/com/mycompany/app/App.java:[79,39] cannot find symbol
symbol: class UpnpServiceImpl
location: class com.mycompany.app.App
The sample code does say: "You need cling-core.jar and its dependencies (seamless-*.jar files) on your classpath to build and run this code. "
But is this not something maven should take care of? If not, how do I include these files?
Here is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>4thline-repo</id>
<url>http://4thline.org/m2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fourthline.cling</groupId>
<artifactId>cling-core</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>
And here is sample code I'm trying to run:
package com.mycompany.app;
import org.fourthline.cling.model.message.header.STAllHeader;
import org.fourthline.cling.model.meta.LocalDevice;
import org.fourthline.cling.model.meta.RemoteDevice;
import org.fourthline.cling.registry.Registry;
import org.fourthline.cling.registry.RegistryListener;
/**
* Runs a simple UPnP discovery procedure.
*/
public class App {
public static void main(String[] args) throws Exception {
// UPnP discovery is asynchronous, we need a callback
RegistryListener listener = new RegistryListener() {
public void remoteDeviceDiscoveryStarted(Registry registry,
RemoteDevice device) {
System.out.println(
"Discovery started: " + device.getDisplayString()
);
}
public void remoteDeviceDiscoveryFailed(Registry registry,
RemoteDevice device,
Exception ex) {
System.out.println(
"Discovery failed: " + device.getDisplayString() + " => " + ex
);
}
public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
System.out.println(
"Remote device available: " + device.getDisplayString()
);
}
public void remoteDeviceUpdated(Registry registry, RemoteDevice device) {
System.out.println(
"Remote device updated: " + device.getDisplayString()
);
}
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
System.out.println(
"Remote device removed: " + device.getDisplayString()
);
}
public void localDeviceAdded(Registry registry, LocalDevice device) {
System.out.println(
"Local device added: " + device.getDisplayString()
);
}
public void localDeviceRemoved(Registry registry, LocalDevice device) {
System.out.println(
"Local device removed: " + device.getDisplayString()
);
}
public void beforeShutdown(Registry registry) {
System.out.println(
"Before shutdown, the registry has devices: "
+ registry.getDevices().size()
);
}
public void afterShutdown() {
System.out.println("Shutdown of registry complete!");
}
};
// This will create necessary network resources for UPnP right away
System.out.println("Starting Cling...");
UpnpService upnpService = new UpnpServiceImpl(listener);
// Send a search message to all devices and services, they should respond soon
upnpService.getControlPoint().search(new STAllHeader());
// Let's wait 10 seconds for them to respond
System.out.println("Waiting 10 seconds before shutting down...");
Thread.sleep(10000);
// Release all resources and advertise BYEBYE to other UPnP devices
System.out.println("Stopping Cling...");
upnpService.shutdown();
}
}
The example code is from: http://4thline.org/projects/cling/core/manual/cling-core-manual.xhtml#chapter.GettingStarted
Would really appreciate your help.
Looks like the example I was using was missing import statements:
import org.fourthline.cling.UpnpService;
import org.fourthline.cling.UpnpServiceImpl;
Maven takes care of the dependencies that you declare in pom.xml, in order to resolve your issue you should add the cling-core dependency in pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>4thline-repo</id>
<url>http://4thline.org/m2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fourthline.cling</groupId>
<artifactId>cling-core</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.fourthline.cling/cling-core -->
<dependency>
<groupId>org.fourthline.cling</groupId>
<artifactId>cling-core</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>
Replace your pom.xml with the above and see maven downloads the jars including all its dependencies.

Servlet with JDBC [duplicate]

This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 5 years ago.
I have a problem with my progect. Files of progect:
House.class
public class House implements Serializable {
//properties -------------------------------------------------------------
private String price;
private String square;
private String RoomNumbers;
//------------------------------------------------------------------------
//getters - settersm Object overriding.... -----------------------------
HouseDAO.class
public class HouseDAO {
Connection connection;
final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb2";
final String DB_USER = "root";
final String DB_PASSWORD = "root";
public HouseDAO(Connection connection) {
this.connection = connection;
}
public List<House> getList() {
List<House> houses = new ArrayList<>();
try {
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
System.out.println("Connection available");
PreparedStatement ps = connection.prepareStatement("SELECT Square, RoomNumbers, Price FROM houses WHERE District = 'Dnepr'");
ResultSet rs = ps.executeQuery();
while (rs.next()){
House house = new House();
house.setSquare(rs.getString("Square"));
house.setRoomNumbers(rs.getString("RoomNumbers"));
house.setPrice(rs.getString("Price"));
houses.add(house);
}
}catch (Exception ex){
System.out.println("SQL exceprion");
ex.printStackTrace();
}
return houses;
}
}
and Servlet:
HousesBaseServlet
#WebServlet("/post")
public class HosesBaseServlet extends HttpServlet {
Connection conn;
private HouseDAO houseDAO;
#Override
public void init(){
houseDAO = new HouseDAO(conn);
}
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
//choice from html form
// String choice = request.getParameter("district");
try {
List<House> houses = houseDAO.getList();
request.setAttribute("houses", houses);
request.getRequestDispatcher("/houses.jsp").forward(request,response);
}catch (Exception ex ) {
System.out.println("Fail to connect with base");
ex.printStackTrace();
}
}
}
I was read some solutiotuns, but it doesn't help. The problem in two exceptions:
SQL exceprion java.sql.SQLException: No suitable driver found for
jdbc:mysql://localhost:3306/mydb2
I try to add:
Class.forName("com.mysql.jdbc.Driver");
to my code, and add mysql connector jar to my project, but it throws exception:
SQL exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Second exception:
JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core
cannot be resolved in either web.xml or the jar files deployed with
this application Fail to connect with base
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
,
JSP taglib:
%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %
and project structure:
[project structure][1]
Project structure - artifacts
In project structure -> libraries i have all jars.
Since you are using IntelliJ I believe you might need to add the libraries to the artifact because from my experience Intellij adds the maven dependencies to the Classpath but not to the artifact.
Make sure you go to File -> Project Structure -> Artifacts and then add all the libraries from the available side to the artifact.
But you need to register the driver before getting the connection otherwise it doesn't work either way :
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
Hope this helps.

Using MS-SQL With Maven2, Netbeans on a (Mac or Linux)

Looking for advice or a bit of help, to point me in the right direction.
I need to connect to a Microsoft SQL Server from a Java program, however, the drivers must be available in maven2 and work with NetBeans.
Any advice? (pointer to an example would be great) (Suicide is no longer an option)
Edit: I've found JTDS- is this a good solution?
Edit 2: Looks like it works... Here is how I have it configured...
Pom file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.steward.ccd</groupId>
<artifactId>amalgainterface</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>amalgainterface</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.ini4j</groupId>
<artifactId>ini4j</artifactId>
<version>0.5.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
</project>
Java File
import java.sql.*;
import org.ini4j.*;
import java.util.*;
import java.io.File;
public class AmalgaInterface {
public static void main(String[] args) {
// MS-SQL Parameters
String db_name = "xxxx";
String db_hostname = "xxxx";
String db_port = "1433";
String db_userid = "xxxx";
String db_password = "xxxx";
String db_timeout = "10";
// Check the Configuration file, and replace all service reference as required.
// Get configuration
String configFile = "/etc/test.conf";
// Load data from INI files
Ini ini = null;
try {
ini = new Ini(new File(configFile));
db_name = ini.get("database", "name");
db_hostname = ini.get("database", "host");
db_userid = ini.get("database", "user");
db_password = ini.get("database", "pass");
db_port = ini.get("database", "port");
db_timeout = ini.get("database", "dbtimeout");
} catch (Exception ex) {
System.out.println("Cannot load the configuration file");
}
// Create the connection string
String db_connect_string = "jdbc:jtds:sqlserver://" + db_hostname + ":" + db_port + "/" + db_name + ";socketTimeout=" + db_timeout;
// setup connection
Connection connection = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection(db_connect_string, db_userid, db_password);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
// clean up
if (connection != null) {
try {
connection.close();
} catch (Exception ex) {
}
}
}
}
Try JTDS
It's a type 4 jdbc driver and I believe it's available via maven repository.

Categories