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`
}
Related
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
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.
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.
I am trying to connect to a DB2 database using SSL on IBM Bluemix.
When I first tried to connect without SSL, it doesn't work. After reading the documentation, I have realized that it connects to the database with SSL enabled.
I tried using the following code to get it connect to the database:
public boolean connect() {
try {
String url = "jdbc:db2://" + serverName + ":" + port + "/" + dbName+
":securityMechanism=9";
connection = DriverManager.getConnection(url, userName, passWord);
st = connection.createStatement();
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
}
return false;
}
Still I am not too sure on how to use the SSL certificate provided with the code above.
I tried searching for examples but most of the explanations are either unclear or used for another database system.
If you are using Liberty, a datasource is generated for you, and you can look it up using jndi.
#Resource(lookup = "jdbc/mydb")
private DataSource myDataSource;
Connection c = myDataSource.getConnection();
"mydb" is the name of the SQLDB service
https://developer.ibm.com/bluemix/2014/02/07/java-db2-10-minutes/
According to the SQLDB documentation, If you use the latest com.ibm.db2.jcc.DB2Driver with the JDBC connection, the current SSL certificate is bundled with the driver and does not need manually installing.
The following snippet shows you how to use the connection details available from VCAP_SERVICES to connect to SQLDB over SSL.
public class SSLTEST {
/**
* #param args
*/
public static void main(String[] args) {
String ServerName = "hostname or IP address";
int PortNumber = 50001;
String DatabaseName = "SQLDB";
String user = "your_user_id_from_VCAP_SERVICES";
String userPassword = "your_password_from_VCAP_SERVICES";
java.util.Properties properties = new java.util.Properties();
properties.put("user", "user ID that has access to SQLDB");
properties.put("password", "password for the user ID that has access to SQLDB");
properties.put("sslConnection", "true");
String url = "jdbc:db2://" + ServerName + ":"+ PortNumber + "/" +
DatabaseName + ":" + traceFileLocation + ";";
java.sql.Connection con = null;
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
}
catch ( Exception e )
{
System.out.println("Error: failed to load Db2 jcc driver.");
}
try
{
System.out.println("url: " + url);
con = java.sql.DriverManager.getConnection(url, properties);
if (con != null) {
System.out.println("Success");
} else {
System.out.println("Failed to make the connection");
}
con.close();
}
catch (Exception e)
{
if (con != null) {
try {
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
e.printStackTrace();
}
}
finally i use datasource to connect to the database.
Context ic = new InitialContext();
DataSource db = (DataSource)context.lookup("jdbc/MyDatabase");
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