Connecting to Azure SQL DB via JTDS on an Android device - java

I'm using JTDS 1.2.7 to connect my Android app to a Windows Azure SQL Server.
Source code:
String connectionString =
"jdbc:jtds:sqlserver://SERVER.database.windows.net:1433;database=demo;" +
"user=test#SERVER;password=PASSWORD;ssl=require";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(connectionString);
Statement st = con.createStatement();
res=st.executeQuery(sql);
When I try to connect to the database I get the
java.lang.NoClassDefError: com.sun.net.ssl.SSLContext
exception.
I tried to add jsse.jar to my build path, but the app just shuts down instead of making connection.
What am I doing wrong?

Related

Not able to connect to Microsoft Azure Mysql Database after deploying in Microsoft Azure

I am trying to connect Azure mysql database. If I use application for local server (Eclipse) it is connecting to Azure MySql Database, once the application is deployed in azure hosting database string is not working.
//connecting to Azure MySql Database from local Application(Eclipse)
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://Hostname:Port/databasename", "username", "password");
//connecting to Azure MySql Database After Deploying Application in Azure(Microsoft Azure)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://Hostname:Port;DatabaseName=databasename;","username","password");
Can anyone tell me how to write connecting String for Hosting Application?

Java 7 SSL connection to SQL Server 2014 from Tomcat

I am trying to connect to SQL Server 2014 with Java 7 via JDBC driver and the error that the app throws is :
The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server returned an incomplete response. The connection has been closed."
My code is:
driver ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
url ="jdbc:sqlserver://Ip:port;databasename=xxx";
user = "xxx";
pass = "xxx";
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pass);
I downloaded the ODBC driver package from MSDN and have tried using each of the following in turn: sqljdbc.jar, sqljdbc4.jar, sqljdbc41.jar, and sqljdbc42.jar, but I got the same error each time.
I resolve the issue using JTDS as driver manager , my code now is :
driver ="net.sourceforge.jtds.jdbc.Driver;
url ="jdbc:jtds:sqlserver://Ip:port;databasename=xxx";
user = "xxx";
pass = "xxx";
Class.forName(driver);
connection = DriverManager.getConnection(url, user, pass);
and i added the lib jtds1.5.1 in my classpath.

How to connect to MySQL database using Java on another PC?

I have made a simple program that uses the connectivity String:
Class.forName("java.sql.DriverManager");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3305/project", "root", "xxxx");
Statement st = conn.createStatement();
Here 'project' is my database name and 'xxxx' is the password.
I have then created an .exe file that works fine on my own computer.
But how can other people on another computer use my program? Do they have to install MySQL? Or Can they access my computer's database on their computer?

Connecting to external database using java

I have a java program that connects to a MS SQL database. The program works perfectly when running through eclipse however I get an error when I run it through AIX:
java.sql.SQLException: Network error IOException: A remote host refused an attempted connect operation.
I am using jtds to connect:
String connectionString = "jdbc:jtds:sqlserver://"+dropez_ip_address+"/"+dropez_db_name;
ResultSet rs = null;
Statement stmt = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionString, dropez_db_username, dropez_db_password);
stmt = conn.createStatement();
}catch(Exception e){}
From jTDS documentation:
Why do I get java.sql.SQLException: "Network error IOException: Connection refused: connect" when trying to get a connection?
The "Connection refused" exception is thrown by jTDS when it is unable to connect to the server. There may be a number of reasons why this could happen:
The server name is misspelled or the port number is incorrect.
SQL Server is not configured to use TCP/IP. Either enable TCP/IP from SQL Server's Network Utility app or have jTDS connect via named pipes (see the URL format for information on how to do this).
There is a firewall blocking port 1433 on the server.
To check whether TCP/IP is enabled and the port is not blocked you can use "telnet 1433". Until telnet doesn't connect, jTDS won't either. If you can't figure out why, ask your network administrator for help.
My bet is your firewall does not allow the AIX host to connect.

Cannot connect to Mysql database using Java

I am running the following code on a Windows Server box using Java on Eclipse.
Connection conn = null; // connection object
Statement stmt = null; // statement object
ResultSet rs = null; // result set object
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/errorcodes", "myusername", "mypassword");
System.out.println ("Database connection established");
}catch (Exception e){
System.err.println ("Cannot connect to database server");
}
And i keep seeing the "Cannot connect to database server error". Any ideas what i might be doing wrong?
I have tried netstat -an and i see :
TCP 127.0.0.1:4464 127.0.0.1:3306 Established
My guess? You haven't got the mysql jdbc connector jar in your classpath. It should be called something like mysql-connector-java-5.1.16-bin.jar, depending on your version of mysql
If you don't have that jar, visit here
Do you sure that it is mysql running on port 3306 and that it's version is supported by your connector/j?
i think you did not start the MySql server in your PC.before running your application
Try the followings:
Clean and rebuild the project in eclipse.
Try to access the mysql database using the username and password in command prompt to ensure the username and password are correct.
If you want to ensure the username and password , you have to query the user table in mysql table again to need another mysql admin account to query.
It is simple .. you need .jar file called mysql-connector-java-5.1.16-bin.jar ,,, download it and add it to your libs ...
good luck !!!

Categories