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?
Related
I´m using eclipse and want to connect to my mysql database. I am new to SQL and thats my first time I want to do something like this. I´ve created two methods, which should connect to a mysql database, which is running on my PC. The first method should create a connection :
public static Connection connectionSQL() {
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/example";
String username = "example";
String password = "1111";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connected!");
} catch(SQLException | ClassNotFoundException e) {
System.out.println("ERROR: Cant connect to database! "+ e);
}
return null;
}
the second should create a table :
public static void createTable() {
try {
Connection connection = connectionSQL();
PreparedStatement ps = connection.prepareStatement("CREATE TABLE IF NOT EXISTS scammerjail(id int NOT NULL AUTO_INCREMENT, Username String, Countdown int, PRIMARY KEY(id))");
ps.executeUpdate();
} catch(SQLException e) {
System.out.println("ERROR: Cant create Table! " + e);
}
finally {
System.out.println("Table created!");
}
}
But everytime I try to run my Code, I get the following message:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client
I´m using maven as build tool and my dependency and its version isnt wrong I think.
This is my code for connecting to the Server remotely.
One thing more i would like to tell you guys that this code works only i am within my home network (the network in which server is installed) but i am unable to access it from other network
hey guys please fix it thanks in advance
public class SQLServerDemo
{
public static void main(String[] args)
{
// TODO code application logic here
try {
System.out.println("Hello from try");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://192.168.0.2:1433;user=demo;password=demo;databaseName=SQL_Demo";
Connection conn = DriverManager.getConnection(url);
if (conn != null)
{
System.out.println("Connected");
} else if (conn == null)
{
System.out.println("NotConnected");
}
String queryString = "select * from UserDetail";
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
}
catch (Exception e)
{
System.out.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
}
Exception
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host GAUTAM-VAIO, port 1433 has failed.
Error: "null. Verify the connection properties.
Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port.
Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
You cannot use 192.168.0.2:1433, it's valid only in your internal LAN network.
You have to find your external IP out of your LAN.
Here's the code I'm using (Oracle database connectivity):
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:#loclahost:1521:XE","system","system");
System.out.println("connection is established");
Statement stmt=conn.createStatement();
int i=stmt.executeUpdate("insert table students ( name varchar2(15),mobile number(10),age varchar2(1))");
System.out.println("Save Sucessfully");
stmt.close();
conn.close();
} catch(Exception e) {
System.out.println(e);
}
this.dispose();
Getting following Error:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could
not establish the connection
You've got a typo in your connection string - use localhost instead of loclahost
use localhost in DriverManager.getConnection
Here is the code :
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","system","system");
System.out.println("connection is established");
Statement stmt=conn.createStatement();
int i=stmt.executeUpdate("insert table students ( name varchar2(15),mobile number(10),age varchar2(1))");
System.out.println("Save Sucessfully");
stmt.close();
conn.close();
}
catch(Exception e) {
System.out.println(e);
}
this.dispose();
}
The error could occur if your JDBC driver is unable to connect to oracle. Check if your oracle service is running and no firewall is blocking your connection.
Check if oracle is listening in port 1521 or not, if not fix the port issue and then try connecting to your database.
Collectively the things to check ,
Check if the listener service is running
No firewall is blocking
Your service is listening on the correct port number that you
specified in your code.
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.
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);