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.
Related
I try connect
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/logins_db","root","")
but it's doesn't work
I use openserver, phpMyAdmin and use Nox emulator.
I tried to connect using the ip address of the android
(172.17.100.2/openserver/logins_db/) because I run through the
emulator
I tried to connect using the ip address of the android
(localhost/openserver/logins_db/) because I run through the emulator
Yes, I enabled internet permission
TextView textView = findViewById(R.id.tv_connect_text);
String text="";
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e){
text += "We don't find your driver!";
e.printStackTrace();
text+=e.toString();
}
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/logins_db","root","");
}
catch (SQLException e){
e.printStackTrace();
text += "Connection failed!\n";
text +=e.toString()+"\n";
}
if(connection!=null){
text += "You are connect! Congratulate";
}
textView.setText(text);
result:
Connction failed!
com.mysql.jdbc.exceptions.jdbc4.MySQLnonTranseitConnectionException:
Could not create connection to database server.]
I know that there was a similar question, but I'm wondering why the application can not find the database?
I have a project in which i connect to my Local Network DB. I Insert,Update and Select from DB, and DB type is SQL Server 2005.
I made a class named ConnectionHelper and everywhere i want to connect to DB, i make an Instance of it and then call its getConnection Method.
This class has a Constructor like this :
public ConnectionHelper() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setConnection(connection);
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
setIpAddress("X.X.X.X:1433");
setDb("XXX");
setUserName("XX");
setPassword("XXXX");
setConnectionURL("jdbc:jtds:sqlserver://" + getIpAddress() + ";"
+ "databaseName=" + getDb() + ";user=" + getUserName()
+ ";password=" + getPassword() + ";");
setConnection(DriverManager.getConnection(getConnectionURL()));
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
}
And this is getConnection Method :
public Connection getConnection() {
return connection;
}
And here is the sample of using this with Callable Statement( Call an Update Stored Procedure) :
ConnectionHelper connectionHelper = new ConnectionHelper();
CallableStatement callableStatement;
try {
callableStatement = connectionHelper.getConnection().prepareCall("{call MySP(?, ?, ?)}");
callableStatement.setInt(1, X);
callableStatement.setInt(2, X));
callableStatement.setInt(3, X);
callableStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
Well, it works good, But Android Devices connect to Local Network via WIFI, and sometimes they could not connect to DB due to Network Problems and then App close it by itself with unfortunately Error.
I want to handle if it could not connect to DB, retry again or display an Toast message to user and do not close the App.
How can i should handle the Exception?
I think you are already catching an exception - but SQLException - so you might find that the exception for connection issue is a Timeout or some other. In this case you need to change the part of your code into something like this:
ConnectionHelper connectionHelper = new ConnectionHelper();
CallableStatement callableStatement;
try {
callableStatement = connectionHelper.getConnection().prepareCall("{call MySP(?, ?, ?)}");
callableStatement.setInt(1, X);
callableStatement.setInt(2, X));
callableStatement.setInt(3, X);
callableStatement.executeUpdate();
} catch (SQLException e) {
//this is a specific SQL exception
e.printStackTrace();
}
catch (Exception e) {
//all other exceptions except the one above
//here you do your toast message
}
Catching the general Exception is good when you do not know all possible exceptions that could be thrown. Please give it a try and let us know if this helps.
Looks like you need an architecture that includes a "internal error" response to the nodes calling your services.
Must not catch the exception in the device, use web api's or webservices to comunicate with DBs.
Now, if your problem is error while processing because the local network on android is down plase look: How to check internet conection on android
I started working today with the JDBC Microsoft SQL Server driver and I am always getting the error that no suitable driver has been found.
Here is my code:
/*
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
*/
String url = "jdbc:sqlserver://" + HOST + ":" + PORT + ";databaseName=" + DATABASE;
try {
connection = DriverManager.getConnection(url, USERNAME, PASSWORD);
} catch (SQLException e) {
System.err.println("[SQL] Error in connection: " + url);
e.printStackTrace();
}
I also used Class.forName(...), but since it will be running on other people's servers I don't want to use this option (This option also did not work). Help would be appreciated, have a nice weekend
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);
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);