import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseAuthException
class RegisterActivity : AppCompatActivity() {
private lateinit var edtRegisterName: EditText
private lateinit var edtRegisterId: EditText
private lateinit var edtRegisterPassword: EditText
private lateinit var btnRegisterRegister: Button
private lateinit var btnRegisterback: Button
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
edtRegisterName = findViewById(R.id.edt_register_name)
edtRegisterId = findViewById(R.id.edt_register_id)
edtRegisterPassword = findViewById(R.id.edt_register_password)
btnRegisterRegister = findViewById(R.id.btn_register_register)
btnRegisterback= findViewById(R.id.btn_register_back)
auth = FirebaseAuth.getInstance()
btnRegisterRegister.setOnClickListener {
val id = edtRegisterId.text.toString()
val password = edtRegisterPassword.text.toString()
val name = edtRegisterName.text.toString()
auth.createUserWithEmailAndPassword(id, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
insertDataToSQLServer(id, password, name)
Toast.makeText(this, "註冊成功", Toast.LENGTH_SHORT).show()
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
} else {
val errorCode = (task.exception as? FirebaseAuthException)?.errorCode
val errorMessage = task.exception?.message ?: "註冊失败"
Toast.makeText(this, "註冊失败:$errorMessage", Toast.LENGTH_SHORT).show()
}
}
}
btnRegisterback.setOnClickListener {
val intent = Intent(this,LoginActivity::class.java)
startActivity(intent)
}
}
private fun insertDataToSQLServer(account: String, password: String, name: String) {
try {
val IP = "*********"
val databaseName = "master"
val username = "****"
val dbPassword = "*******"
val connstr = "jdbc:jtds:sqlserver://$IP/$databaseName"
var connection: Connection? = null
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver")
connection = DriverManager.getConnection(connstr, username, dbPassword)
// Set auto-commit to false to start a transaction
connection.autoCommit = false
val insertQuery = "INSERT INTO dbo.Vip (V_name, V_account, V_password) VALUES (?, ?, ?);"
val preparedStatement: PreparedStatement = connection.prepareStatement(insertQuery)
preparedStatement.setString(1, name)
preparedStatement.setString(2, account)
preparedStatement.setString(3, password)
preparedStatement.executeUpdate()
// Commit the transaction
connection.commit()
println("Data inserted successfully!")
} catch (e: Exception) {
e.printStackTrace()
// Rollback the transaction in case of exception
connection?.rollback()
} finally {
// Reset auto-commit to true and close the connection
connection?.autoCommit = true
connection?.close()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
先在以下這段加上顯示錯誤訊息(註)
看看是什麼錯誤
try {
...
println("Data inserted successfully!")
} catch (e: Exception) {
e.printStackTrace()//加上用 TOAST,手機才看得到
// Rollback the transaction in case of exception
connection?.rollback()
} finally {
// Reset auto-commit to true and close the connection
connection?.autoCommit = true
connection?.close()
}
} catch (e: Exception) {
e.printStackTrace()//加上用 TOAST,手機才看得到
}
註:類似以下這段
val errorCode = (task.exception as? FirebaseAuthException)?.errorCode
val errorMessage = task.exception?.message ?: "註冊失败"
Toast.makeText(this, "註冊失败:$errorMessage", Toast.LENGTH_SHORT).show()