AsyncTask 中 PreparedStatement 的游标实现
Cursor implement on PreparedStatement in AsyncTask
所以我正在尝试为我的 class 实现一个游标,以便我可以填充我的数据网格(当我需要为 SP 和准备好的语句使用可调用时,不确定这是否是正确的方法)这是我用这个 guide here 做的,但我得到了一个
DataGridActivity.Itemnumber is not abstract and does not override abstract method respond(Bundle) in Cursor
我不确定如何在这里实现抽象方法,因为我
通常对我准备好的语句使用 getstring 方法想知道是否有某种方法可以代替填充我的网格而不是使用 Cursor
public class Itemnumber extends AsyncTask<String,String,String> implements Cursor {
String z = "";
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String r) {
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
PreparedStatement preparedStatement = null;
String sqli = "select ID,ItemNumber,Trashed,Sold from [file].[Item] where [ItemNumber] =?";
preparedStatement = con.prepareStatement(sqli);
preparedStatement.setString(1, "test");
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
} else {
}
}
} catch (Exception ex) {
z = "Exceptions";
}
return z;
}
@Override
public Bundle respond(Bundle extras){
moveToFirst();
return Bundle.EMPTY;
}
}
我的数据网格activityclass
Cursor csr = new Itemnumber();
//create DataTable object
DataTable dtDataSource = new DataTable();
//define column
dtDataSource.addAllColumns(new String[]{"column_1", "column_2","column_3", "column_4});
//create DataRow
DataTable.DataRow drRow;
//populate data from cursor into DataSource
if(csr.moveToFirst()){
do{
drRow = dtDataSource.newRow();
drRow.set("column_1", csr.getString(csr.getColumnIndex("field_1")));
drRow.set("column_2", csr.getString(csr.getColumnIndex("field_2")));
drRow.set("column_2", csr.getString(csr.getColumnIndex("field_3")));
drRow.set("column_4", csr.getString(csr.getColumnIndex("field_4")));
dtDataSource.add(drRow);
} while(csr.moveToNext());
csr.close();
}
/**
* Prepare the DataGrid
*/
//initialize DataGrid
DataGrid dg = (DataGrid)findViewById(R.id.datagrid);
//define column style, bond each DataGrid column by DataTable column
dg.addColumnStyles(new DataGrid.ColumnStyle[]{
new DataGrid.ColumnStyle(getString(R.string.ID), "column_1", 80),
new DataGrid.ColumnStyle(getString(R.string.ItemNumber), "column_2", 120),
new DataGrid.ColumnStyle(getString(R.string.Trashed), "column_3", 100),
new DataGrid.ColumnStyle(getString(R.string.Sold), "column_4", 150)
});
//set the DataTable as source
dg.setDataSource(dtDataSource);
//generate the DataGrid
dg.refresh();
您必须覆盖 Itemnumber
class 中的最后一个方法。
此方法在interface Cursor
中,命名为:respond(Bundle)
因此,如果您的 class Itemnumber
实施了 Cursor
,请添加
@Override
Bundle respond(Bundle extras){
return Bundle.EMPTY;
}
并实施,你的方法必须return
我想出了如何使用准备好的语句来做到这一点。
因为实现所有的抽象方法和接口似乎都不符合逻辑
public class DataGridActivity extends Activity {
/** Called when the activity is first created. */
ConnectionClass connectionClass;
//define column
DataTable.DataRow drRow;
DataTable dtDataSource = new DataTable();
Button btnsearch;
DataGrid dg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datagridt);
dg = (DataGrid)findViewById(R.id.datagrid);
btnsearch = (Button) findViewById(R.id.btnsearch);
connectionClass = new ConnectionClass(this.getApplicationContext());
/**
* Prepare the DataGrid
*/
//initialize DataGrid
//define column style, bond each DataGrid column by DataTable column
dg.addColumnStyles(new DataGrid.ColumnStyle[]{
new DataGrid.ColumnStyle(getString(R.string.dito_nr), "column_1", 80),
new DataGrid.ColumnStyle(getString(R.string.Biltype), "column_2", 120),
new DataGrid.ColumnStyle(getString(R.string.kort_nr), "column_3", 100),
new DataGrid.ColumnStyle(getString(R.string.Del_Type), "column_4", 150)
});
}
public class Itemnumber extends AsyncTask<String,String,String> {
String z = "";
@Override
protected void onPreExecute() {
dtDataSource.addAllColumns(new String[]{"column_1", "column_2","column_3", "column_4"});
drRow = dtDataSource.newRow();
dtDataSource.add(drRow);
dg.setDataSource(dtDataSource);
dg.refresh();
}
@Override
protected void onPostExecute(String r) {
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
PreparedStatement preparedStatement = null;
String sqli = "select ID,ItemNumber,Trashed,Sold from [file].[Item]";
preparedStatement = con.prepareStatement(sqli);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
//create DataRow
drRow.set("column_1", rs.getString(1));
drRow.set("column_2", rs.getString(2));
drRow.set("column_3", rs.getString(3));
drRow.set("column_4", rs.getString(4));
} else {
}
}
} catch (Exception ex) {
z = "Exceptions";
}
return z;
}
}
public void btnsearch (View view) {
// TODO Auto-generated method stub
Itemnumber item = new Itemnumber();
item.execute("");
}
}
所以我正在尝试为我的 class 实现一个游标,以便我可以填充我的数据网格(当我需要为 SP 和准备好的语句使用可调用时,不确定这是否是正确的方法)这是我用这个 guide here 做的,但我得到了一个
DataGridActivity.Itemnumber is not abstract and does not override abstract method respond(Bundle) in Cursor
我不确定如何在这里实现抽象方法,因为我 通常对我准备好的语句使用 getstring 方法想知道是否有某种方法可以代替填充我的网格而不是使用 Cursor
public class Itemnumber extends AsyncTask<String,String,String> implements Cursor {
String z = "";
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String r) {
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
PreparedStatement preparedStatement = null;
String sqli = "select ID,ItemNumber,Trashed,Sold from [file].[Item] where [ItemNumber] =?";
preparedStatement = con.prepareStatement(sqli);
preparedStatement.setString(1, "test");
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
} else {
}
}
} catch (Exception ex) {
z = "Exceptions";
}
return z;
}
@Override
public Bundle respond(Bundle extras){
moveToFirst();
return Bundle.EMPTY;
}
}
我的数据网格activityclass
Cursor csr = new Itemnumber();
//create DataTable object
DataTable dtDataSource = new DataTable();
//define column
dtDataSource.addAllColumns(new String[]{"column_1", "column_2","column_3", "column_4});
//create DataRow
DataTable.DataRow drRow;
//populate data from cursor into DataSource
if(csr.moveToFirst()){
do{
drRow = dtDataSource.newRow();
drRow.set("column_1", csr.getString(csr.getColumnIndex("field_1")));
drRow.set("column_2", csr.getString(csr.getColumnIndex("field_2")));
drRow.set("column_2", csr.getString(csr.getColumnIndex("field_3")));
drRow.set("column_4", csr.getString(csr.getColumnIndex("field_4")));
dtDataSource.add(drRow);
} while(csr.moveToNext());
csr.close();
}
/**
* Prepare the DataGrid
*/
//initialize DataGrid
DataGrid dg = (DataGrid)findViewById(R.id.datagrid);
//define column style, bond each DataGrid column by DataTable column
dg.addColumnStyles(new DataGrid.ColumnStyle[]{
new DataGrid.ColumnStyle(getString(R.string.ID), "column_1", 80),
new DataGrid.ColumnStyle(getString(R.string.ItemNumber), "column_2", 120),
new DataGrid.ColumnStyle(getString(R.string.Trashed), "column_3", 100),
new DataGrid.ColumnStyle(getString(R.string.Sold), "column_4", 150)
});
//set the DataTable as source
dg.setDataSource(dtDataSource);
//generate the DataGrid
dg.refresh();
您必须覆盖 Itemnumber
class 中的最后一个方法。
此方法在interface Cursor
中,命名为:respond(Bundle)
因此,如果您的 class Itemnumber
实施了 Cursor
,请添加
@Override
Bundle respond(Bundle extras){
return Bundle.EMPTY;
}
并实施,你的方法必须return
我想出了如何使用准备好的语句来做到这一点。
因为实现所有的抽象方法和接口似乎都不符合逻辑
public class DataGridActivity extends Activity {
/** Called when the activity is first created. */
ConnectionClass connectionClass;
//define column
DataTable.DataRow drRow;
DataTable dtDataSource = new DataTable();
Button btnsearch;
DataGrid dg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datagridt);
dg = (DataGrid)findViewById(R.id.datagrid);
btnsearch = (Button) findViewById(R.id.btnsearch);
connectionClass = new ConnectionClass(this.getApplicationContext());
/**
* Prepare the DataGrid
*/
//initialize DataGrid
//define column style, bond each DataGrid column by DataTable column
dg.addColumnStyles(new DataGrid.ColumnStyle[]{
new DataGrid.ColumnStyle(getString(R.string.dito_nr), "column_1", 80),
new DataGrid.ColumnStyle(getString(R.string.Biltype), "column_2", 120),
new DataGrid.ColumnStyle(getString(R.string.kort_nr), "column_3", 100),
new DataGrid.ColumnStyle(getString(R.string.Del_Type), "column_4", 150)
});
}
public class Itemnumber extends AsyncTask<String,String,String> {
String z = "";
@Override
protected void onPreExecute() {
dtDataSource.addAllColumns(new String[]{"column_1", "column_2","column_3", "column_4"});
drRow = dtDataSource.newRow();
dtDataSource.add(drRow);
dg.setDataSource(dtDataSource);
dg.refresh();
}
@Override
protected void onPostExecute(String r) {
}
@Override
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
PreparedStatement preparedStatement = null;
String sqli = "select ID,ItemNumber,Trashed,Sold from [file].[Item]";
preparedStatement = con.prepareStatement(sqli);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
//create DataRow
drRow.set("column_1", rs.getString(1));
drRow.set("column_2", rs.getString(2));
drRow.set("column_3", rs.getString(3));
drRow.set("column_4", rs.getString(4));
} else {
}
}
} catch (Exception ex) {
z = "Exceptions";
}
return z;
}
}
public void btnsearch (View view) {
// TODO Auto-generated method stub
Itemnumber item = new Itemnumber();
item.execute("");
}
}