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