gpt4 book ai didi

java - statements.executeQuery() 不起作用

转载 作者:行者123 更新时间:2023-12-01 17:08:07 24 4
gpt4 key购买 nike

statement = conRecieved.createStatement();
resultSet = statement.executeQuery("Select * from actor");

在这个if中我检查它是否返回任何结果,但它不输入这个if

if (resultSet.next()) {
Toast.makeText (getActivity (), "ResultsetWorks", Toast.LENGTH_SHORT) .show ();
} else {
Toast.makeText (getActivity (), "REsult set no results", Toast.LENGTH_SHORT) .show ();
}

连接数据库的过程是在一个AsynTask类中执行的,它给我连接,创建的Connection对象,错在哪里?我尝试强制转换收到的连接,但不起作用。

AsyncTaskConexion asyncTaskConexion = new AsyncTaskConexion(getActivity(), new GetConnection() {
@Override
public void getConnection(Connection conexion) {
if(conexion!=null){
Connection conRecieved = (Connection )conexion;

最佳答案

I fixed it by running the query in a separate thread, not using the mainthread. I do not know if this is the best solution but it works.

AsyncTaskConexion asyncTaskConexion = new AsyncTaskConexion(getActivity(), new GetConnection() {
@Override
public void getConnection(Connection conexion) {
if(conexion!=null){
Connection conexionRecibida = conexion;
new Thread(new Runnable() {
@Override
public void run() {
try {
Statement statement = conexionRecibida.createStatement();
ResultSet resultSet = statement.executeQuery("Select * from actor");

with the method runOnUiThread(... you can iteract with the Ui elements

                            getActivity().runOnUiThread(new Runnable() {
public void run() {
try {
if(resultSet.next()){
Toast.makeText(getActivity(),"ResultsetWorks", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),"Empty ResultSet", Toast.LENGTH_SHORT).show();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});

关于java - statements.executeQuery() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61445940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com