Android Studio connecting to SQL Database - java

I am trying to connect Android studio app to SQL server (Heidi SQL) I have the jtds jars (jtds-1.2.7.jar) file inside the lib and added the dependency. It still doesnt read the data and show the result from the database and I have this error:
E/Error: net.sourceforge.jtds.jdbc.Driver
enter image description here
These are my codes.
public class ConnectionHelper {
Connection con;
String uname, pass, ip, port, database;
#SuppressLint("NewApi")
public Connection connectionClass() {
ip = "127.0.0.1";
database = "eat";
uname = "root";
pass = "pass";
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:mysqlserver://" + ip + "/" + database + ";user=" + uname +
";password=" + pass +";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (Exception ex){
Log.e("Error" , ex.getMessage());
}
return connection;
} }

Looks like you are getting rejected using port 54015. Is that the port the SQL Server is listening to for JDBC connections? If not, you may need to append the port# to ConnectionClass.ip i.e. 127.0.0.1:[port number goes here] aka 127.0.0.1:12345

Related

Errors while connecting MSSQL Server to android application

I am trying to connect my Android application to MSSQL server which is installed on my PC. I have previously been successful with connecting them but for some reason now I am getting a network error. I know that a direct connection shouldn't be made and a web service should be used but I am required to make a JDBC connection to the database.
The error I am getting is:
Network error IOException: failed to connect to /192.168.0.101 (port
1433) from /192.168.0.100 (port 37632)
I have configured my MSSQL Server to listen on port 1433.
This is the connection class.
package com.example.resourceapplication.LogIn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
public class ConnectionClass {
static String ip = "192.168.0.101";
static String classs = "net.sourceforge.jtds.jdbc.Driver";
static String db = "EKANBAN";
static String username = "root";
static String password = "root";
public static Connection CONN(){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + username + ";password="
+ password + ";";
DriverManager.setLoginTimeout(2);
conn = DriverManager.getConnection(ConnURL);
Log.d("Hello","It Worked");
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
return null;
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
return null;
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
return null;
}
return conn;
}
}
This class had worked for me previously but for some reason doesn't work now. Any help would be appreciated.

SQL exception: No suitable driver while using MS JDBC in Android Studio

I'm trying using MS JDBC to connect to my SQL Server database but encountering this error.
java.sql.SQLException: No suitable driver
Here is my code
#SuppressLint("NewAPI")
public void Connect() throws Exception {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
connection = null;
String S_url = null;
String driver = null;
try {
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
Class.forName(driver);
S_url = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ pass + ";";
connection = DriverManager.getConnection(S_url);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
I already added a .jar file into my project
Jar file
And set a dependency
Dependencies setting
Really appreciate your help and sorry if my English bother you, it's not my native language
Remove the jtds: from your JDBC URL that you assign to S_url. You register a Microsoft driver, but use the URL format of a jTDS driver.

connecting a java app to external microsoft sql server 2012

I have created a SQL server 2012 database. I need to connect to the database by using Java app created on another pc. this is my code but I cannot connect to the database, and I get error: "Login failed. The login is from an untrusted domain and cannot be used with Windows Authentication." (my code is working when both Java app and SQL server running on the same PC).
Appreciate your help.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String jdbcUrl = "jdbc:sqlserver://THINKPADPC:1433;databaseName=TestDB;integratedSecurity=true;";
conn = DriverManager.getConnection(jdbcUrl);
Have you tried sql server authentication. And pass username and password.
If you tryin windows authentication then it might be taking credentials from your(java) machine which has not been giving access on the hosted sql server machine.
Please Try this Connection and Change the ip, db , sa and password.
public class ConnectionClass {
String ip = "192.168.0.131";
String classs = "net.sourceforge.jtds.jdbc.Driver";
String db = "Andro";
String un = "sa";
String password = "Admnsql1~";
#SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName(classs);
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
+ "databaseName=" + db + ";user=" + un + ";password="
+ password + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return conn;
}`enter code here`
}

Cannot connect Java to external mysql database

Hello i am currently building a programm that register users to mysql database. Everything works fine on localhost but when i try to connect to external database it gives me an error such as this below.
I have granted all privileges to the user in the database i am trying to acess and also i have the driver installed. Any ideas??
My code:
private void createEventListenerDBProperties() {
dbSubmitBtn.addActionListener((ActionEvent e) -> {
if (dbDriverChooser.getSelectedItem().equals("com.mysql.jdbc.Driver")) {
driver = (String) dbDriverChooser.getSelectedItem();
port = dbPortField.getText();
host = "jdbc:mysql://" + hostField.getText() + ":" + port + "/";
db = dbnameField.getText();
dbuser = dbUsernameField.getText();
dbpassword = new String(dbPasswordField.getPassword());
}
}
});
}
private Connection instanciateDB() {
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(host + db, dbuser, dbpassword);
System.out.println("Connection Established");
} catch (ClassNotFoundException | SQLException e) {
System.out.println("Connection not Established");
JOptionPane.showMessageDialog(Project2.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
return con;
}
I think your way to connect is wrong. Instead of this:
con = DriverManager.getConnection(host + db, dbuser, dbpassword);
try this:
con = DriverManager.getConnection(host + db + "?user=" + dbuser + "&password=" +dbpassword);
Check this out, it may be useful.
http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

How to make a MySQL Connection in JAVA

I can't seem to get a connection made on my Java program to java, I have started the MySQL Server, made a database in phpMyAdmin. But I'm confused on how I should use the JDBC Driver that I downloaded from MySQL.
Here's my code:
private void startConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost/";
String dbName = "bank";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
System.out.println("NO CONNECTION =(");
}
}
I had included the jar file in my JDK - jre\lib\ext folder but nothing. Any ideas?
Thanks in advance.
One thing stands out: you haven't specified a network port in the URL. The default port is 3306. So try:
jdbc:mysql://localhost:3306/
For the URL.
You have to specify the port. It's String url = "jdbc:mysql://localhost:3306/"; by default.
private void startConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "bank";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "password";
try
{
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}
catch (Exception e)
{
System.out.println("NO CONNECTION =(");
}
}
This will work

Categories