iT邦幫忙

0

請問資料為何傳不到SQLserver

  • 分享至 

  • xImage

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()
    }
}

}

別學這個手機直接連資料庫的錯誤教學~
因為APP可以反編譯原始碼的~
你的資料IP帳號密碼都會被盜走~
正確方式是呼叫網站API取得資料~
用API相互APP運作~
同意樓上。手機要練習跟資料庫的CRUD,玩玩SQLite就行了。
同意樓上上
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2023-08-25 07:18:48

先在以下這段加上顯示錯誤訊息(註)
看看是什麼錯誤

        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()

我要發表回答

立即登入回答