0

I´m traying to create app in Android studio thank this app connect to my hosting server and to do any operation with the data.

I have a button that instanciate a kotlin class that contain method to connect DB and function to do operation.

My problem it´s that when i do click in my button my function connect return this message:

I/System.out: java.sql.SQLNonTransientConnectionException: Could not create connection to database server.

i´m using JDBC Driver to connect to my DB, in the future i do a web service to do this.

My function connect have this:

class MySQLDatabaseConnector {
    internal var conn: Connection? = null
    internal var username = "root" // provide the username
    internal var password = "" // provide the corresponding password

    fun connect() {
        val JDBC_DRIVER = "com.mysql.jdbc.Driver"
        val DB_URL = "jdbc:mysql://192.167.1.108:3308/prueba"
        val USER = "root"
        val PASSWORD = ""
        try {
            Class.forName(JDBC_DRIVER).newInstance()
            conn = DriverManager.getConnection(DB_URL, USER, PASSWORD)
            /*ar stmt = conn!!.createStatement()
            var resultSet = stmt.executeQuery("SELECT * FROM users")

            while(resultSet.next()){
                var record = resultSet.getString(1) + " " + resultSet.getString(2) + "\n"

                println(record)
            }

            println(stmt)*/
        } catch (ex: SQLException) {
            // handle any errors
            println(ex.toString())
        } catch (ex: Exception) {
            // handle any errors
            println(ex.toString())
        }
    }

and my activity main its:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login);

        val buttonLogin = findViewById(R.id.btLogin) as Button

        buttonLogin.setOnClickListener{
            val connect = MySQLDatabaseConnector()

            connect.connect()
        }
    }

i have my connector in lib:

mysql-connector-java-8.0.27.jar

i´m using this. I dowloaded it from this URL with software for windows:

https://dev.mysql.com/downloads/connector/j/5.1.html

i hope that anybody can help me.

Thanks you for readme and sorry for my english

scorpions78
  • 553
  • 4
  • 17
  • Looks like you are accessing system from emulator ,Refer [this](https://stackoverflow.com/questions/5528850/how-do-you-connect-localhost-in-the-android-emulator) – Anshul Nov 13 '21 at 11:05
  • @Anshul thanks for your response i´m traying this response. i was configured my emulator in 10.0.2.2:80 because my wamp it´s in port 80 in my code i´m traying in this direction with port 3308 but same error. it´s i change this direction to 127.0.0.1 and proxy, same error. Now response with this message: This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. – scorpions78 Nov 13 '21 at 12:24

0 Answers0