學習時間2
程式碼量350
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.WindowDecorActionBar;
import com.example.myapplication.Utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class line extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> queryResults = new ArrayList<>();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
Button button5 = findViewById(R.id.button5);
Button button6 = findViewById(R.id.button6);
Button button7 = findViewById(R.id.button7);
Button button8 = findViewById(R.id.button8);
Button button9 = findViewById(R.id.button9);
Button button10 = findViewById(R.id.button10);
Button button11 = findViewById(R.id.button11);
Button button12 = findViewById(R.id.button12);
Button button13 = findViewById(R.id.button13);
Button button14 = findViewById(R.id.button14);
Button button15 = findViewById(R.id.button15);
Button button16 = findViewById(R.id.button16);
Button button17 = findViewById(R.id.button17);
Button button18 = findViewById(R.id.button18);
Button button19 = findViewById(R.id.button19);
Button button20 = findViewById(R.id.button20);
Button button21 = findViewById(R.id.button21);
Button button22 = findViewById(R.id.button22);
Button button23 = findViewById(R.id.button23);
Button button24 = findViewById(R.id.button24);
// 為每個按鈕設定點選事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 1");
}
}).start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 2");
}
}).start();
}
});
// 新增更多按鈕的點選事件處理,以此類推
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 3");
}
}).start();
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 4");
}
}).start();
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 5");
}
}).start();
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 6");
}
}).start();
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 7");
}
}).start();
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 8");
}
}).start();
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 9");
}
}).start();
}
});
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 10");
}
}).start();
}
});
button11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 11");
}
}).start();
}
});
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 12");
}
}).start();
}
});
button13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 13");
}
}).start();
}
});
button14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 14");
}
}).start();
}
});
button15.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 15");
}
}).start();
}
});
button16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 16");
}
}).start();
}
});
button17.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 17");
}
}).start();
}
});
button18.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 18");
}
}).start();
}
});
button19.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 19");
}
}).start();
}
});
button20.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 20");
}
}).start();
}
});
button21.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 21");
}
}).start();
}
});
button22.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 22");
}
}).start();
}
});
button23.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 23");
}
}).start();
}
});
button24.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 24");
}
}).start();
}
});
}
public void performLineQuery(String query) {
try {
// Get connection using JDBCUtils
Connection conn = JDBCUtils.getConn();
if (conn != null) {
System.out.println("Connected to the database");
// Create statement
Statement statement = conn.createStatement();
// Execute query
ResultSet resultSet = statement.executeQuery(query);
// Process result set
while (resultSet.next()) {
// Process query result
// For example, print site_info value
System.out.println(resultSet.getString("site"));
}
// Close resources
resultSet.close();
statement.close();
conn.close();
} else {
System.out.println("Failed to connect to the database");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.WindowDecorActionBar;
import com.example.myapplication.Utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class line extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> queryResults = new ArrayList<>();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
Button button4 = findViewById(R.id.button4);
Button button5 = findViewById(R.id.button5);
Button button6 = findViewById(R.id.button6);
Button button7 = findViewById(R.id.button7);
Button button8 = findViewById(R.id.button8);
Button button9 = findViewById(R.id.button9);
Button button10 = findViewById(R.id.button10);
Button button11 = findViewById(R.id.button11);
Button button12 = findViewById(R.id.button12);
Button button13 = findViewById(R.id.button13);
Button button14 = findViewById(R.id.button14);
Button button15 = findViewById(R.id.button15);
Button button16 = findViewById(R.id.button16);
Button button17 = findViewById(R.id.button17);
Button button18 = findViewById(R.id.button18);
Button button19 = findViewById(R.id.button19);
Button button20 = findViewById(R.id.button20);
Button button21 = findViewById(R.id.button21);
Button button22 = findViewById(R.id.button22);
Button button23 = findViewById(R.id.button23);
Button button24 = findViewById(R.id.button24);
// 為每個按鈕設定點選事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 1");
}
}).start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 2");
}
}).start();
}
});
// 新增更多按鈕的點選事件處理,以此類推
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 3");
}
}).start();
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 4");
}
}).start();
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 5");
}
}).start();
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 6");
}
}).start();
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 7");
}
}).start();
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 8");
}
}).start();
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 9");
}
}).start();
}
});
button10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 10");
}
}).start();
}
});
button11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 11");
}
}).start();
}
});
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 12");
}
}).start();
}
});
button13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 13");
}
}).start();
}
});
button14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 14");
}
}).start();
}
});
button15.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 15");
}
}).start();
}
});
button16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 16");
}
}).start();
}
});
button17.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 17");
}
}).start();
}
});
button18.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 18");
}
}).start();
}
});
button19.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 19");
}
}).start();
}
});
button20.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 20");
}
}).start();
}
});
button21.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 21");
}
}).start();
}
});
button22.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 22");
}
}).start();
}
});
button23.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 23");
}
}).start();
}
});
button24.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
performLineQuery("SELECT site FROM sub_find WHERE line = 24");
}
}).start();
}
});
}
public void performLineQuery(String query) {
try {
// Get connection using JDBCUtils
Connection conn = JDBCUtils.getConn();
if (conn != null) {
System.out.println("Connected to the database");
// Create statement
Statement statement = conn.createStatement();
// Execute query
ResultSet resultSet = statement.executeQuery(query);
// Process result set
while (resultSet.next()) {
// Process query result
// For example, print site_info value
System.out.println(resultSet.getString("site"));
}
// Close resources
resultSet.close();
statement.close();
conn.close();
} else {
System.out.println("Failed to connect to the database");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}