1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| class MainActivity : AppCompatActivity() {
// Declaring layout button, edit texts
var login: Button? = null
var username: EditText? = null
var password: EditText? = null
var progressBar: ProgressBar? = null
var email: EditText?=null
var setUsrActive: CheckBox?=null
// End Declaring layout button, edit texts
// Declaring connection variables
var con: Connection? = null
var un: String? = null
var pass: String? = null
var db: String? = null
var ip: String? = null
//End Declaring connection variables
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Getting values from button, texts and progress bar
login = findViewById<View>(R.id.button) as Button
username = findViewById<View>(R.id.txtUsername) as EditText
password = findViewById<View>(R.id.txtPassword) as EditText
progressBar = findViewById<View>(R.id.progressBar) as ProgressBar
email= findViewById<View>(R.id.txtMail) as EditText
setUsrActive=findViewById<View>(R.id.chkActive) as CheckBox
// End Getting values from button, texts and progress bar
// Declaring Server ip, username, database name and password
ip = "172.23.26.81:1433"
db = "MaBDD"
un = "Utilisateur"
pass = "MDP123"
// Declaring Server ip, username, database name and password
// Setting up the function when button login is clicked
login!!.setOnClickListener {
val checkLogin = CheckLogin() // this is the Asynctask, which is used to process in background to reduce load on app process
checkLogin.execute("")
}
//End Setting up the function when button login is clicked
}
inner class CheckLogin :
AsyncTask<String?, String?, String?>() {
private var z: String? = ""
private var isSuccess = false
override fun onPreExecute() {
progressBar!!.visibility = View.GONE
}
override fun onPostExecute(r: String?) {
val username = username!!.text.toString()
val password = password!!.text.toString()
val mail =email!!.text.toString()
val usrIsActive=setUsrActive!!.isChecked
if (username.trim { it <= ' ' } == "" || password.trim { it <= ' ' } == "")
Toast.makeText(this@MainActivity, "Please enter username and password.", Toast.LENGTH_LONG) .show()
else {
try {
con = sqlAgent(un.toString(),pass.toString(),db.toString(), ip.toString()) // Connect to database
if (con == null) {
Toast.makeText(this@MainActivity, "Please check your network connections.", Toast.LENGTH_LONG) .show()
}
else
{
progressBar!!.visibility = View.VISIBLE
// Change below query according to your own database.
val query = "INSERT INTO [ArcgisData].[dbo].[tblUSERS] VALUES ('$username','$password','$mail','$usrIsActive')"
val stmt = con!!.createStatement()
stmt.executeUpdate(query)
progressBar!!.visibility = View.GONE
Toast.makeText(this@MainActivity, "User added successfully!", Toast.LENGTH_LONG) .show()
}
} catch (ex: Exception) {
Toast.makeText(this@MainActivity, ex.message, Toast.LENGTH_LONG) .show()
}
}
}
override fun doInBackground(vararg p0: String?): String? {
return null
}
}
@SuppressLint("NewApi")
fun sqlAgent(user: String, password: String, database: String, server: String): Connection? {
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)
var connection: Connection? = null
var ConnectionURL: String?=null
try {
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver") // To use Microsoft JDBC Driver
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance() // to use native Jtds Driver
// ConnectionURL="jdbc:sqlserver://$server;user=$user;password=$password" // Microsoft JDBC Connection URL
ConnectionURL = "jdbc:jtds:sqlserver://$server;database=$database;user=$user;password=$password;" // Jtds Connection URL
connection = DriverManager.getConnection(ConnectionURL)
} catch (se: SQLException) {
Log.e("error here 1 : ", se.message)
} catch (e: ClassNotFoundException) {
Log.e("error here 2 : ", e.message)
} catch (e: Exception) {
Log.e("error here 3 : ", e.message)
}
return connection
} |
Partager