MainActivity.java:
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class MainActivity extends AppCompatActivity { String hy=null; Handler handler=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnl=findViewById(R.id.btnSeek); handler=new Handler(); btnl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { try{ String driverName = "net.sourseforge.jtds.jdbc.Driver"; String dbURL = "jdbc:jtds:sqlserver://xx.xxx.x.xx:xxxx;DatabaseName=单词表"; String userName ="tempsql"; String userPwd = "tempsql"; Connection con; EditText etl = findViewById(R.id.editText); String dc = etl.getText().toString(); Class.forName(driverName); con = DriverManager.getConnection(dbURL,userName,userPwd); String sql = "select * from [六级] where english‘" + dc +"’"; Statement st =con.createStatement(); ResultSet rs = st.executeQuery(sql); if (rs.next()){ hy=rs.getString("chinese"); } else{ hy="没找到"; } rs.close(); st.close(); con.close(); handler.post(runnable); } catch (Exception e) { e.printStackTrace(); } } }).start(); } }); } Runnable runnable=new Runnable() { @Override public void run() { EditText et2 = findViewById(R.id.editText2); et2.setText(hy); } }; }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnSeek" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="182dp" android:layout_marginEnd="62dp" android:text="Button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="184dp" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginBottom="8dp" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>