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.
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 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 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");
As said in the title of the question, JDBC seems to work only in test classes, can't explain why.
public static Connection getConnection()
{
try {
String connectionString = "jdbc:mysql://localhost/" + database + "?" +
"user=" + sqlUser + "&password=" + sqlPassword;
return DriverManager.getConnection(connectionString);
} catch (SQLException ex) {
Logger.getLogger(ConnectionLoader.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
The following test passes:
assertNotNull("Connection must not be null.", ConnectionLoader.getConnection());
But when debugging the project fails with SQLException saying no suitable driver found.
Here's a screenshot of my project in Netbeans, as you can see I included the mysql jar in the libraries.
Am I missing something?
You need to load MySQL driver.
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://x.x.x.x:3306/databasename", "username", "password");
...
}catch(Exception ex){
ex.printStackTrace();
}
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`
}