作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到一个问题,即我的 sql 查询无法根据 Edittext(txt_Id) 中设置的值从 MSSQL 检索特定用户数据。应用程序可以显示我的 txt_Id 值,但无法从数据库中检索与该值相对应的数据。如果我错过了什么,请帮助并通知我。
package com.example.myapplication;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.lang.String;
public class Operation extends AppCompatActivity {
ConnectionClass connectionClass;
EditText txt_MO,txt_Material,txt_Machine,txt_PFNO, txt_pic, txt_Id;
Button btn_start;
ProgressBar pbbar;
TextView lblName;
UserSessionManager session;
Button btn_logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_operation);
session = new UserSessionManager(getApplicationContext());
EditText txt_Id = (EditText) findViewById(R.id.txt_Id);
// btn_logout = (Button) findViewById(R.id.btn_logout);
Toast.makeText(getApplicationContext(),
"User Login Status: " + session.isUserLoggedIn(),
Toast.LENGTH_LONG).show();
// Check user login (this is the important point)
// If User is not logged in , This will redirect user to LoginActivity
// and finish current activity from activity stack.
if(session.checkLogin())
finish();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
// get name
String pfno = user.get(UserSessionManager.KEY_PF);
// get email
String email = user.get(UserSessionManager.KEY_EMAIL);
// Show user data on activity
txt_Id.setText(Html.fromHtml("Name: <b>" + pfno + "</b>"));
// btn_logout.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View arg0) {
//
// // Clear the User session data
// // and redirect user to LoginActivity
// session.logoutUser();
// }
// });
connectionClass = new ConnectionClass();
txt_MO = (EditText) findViewById(R.id.txt_MO);
txt_Material = (EditText) findViewById(R.id.txt_Material);
txt_Machine = (EditText) findViewById(R.id.txt_Machine);
txt_PFNO = (EditText) findViewById(R.id.txt_PFNO);
btn_start = (Button) findViewById(R.id.btn_start);
txt_pic = (EditText) findViewById(R.id.txt_pic);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddPro addPro = new AddPro();
addPro.execute("");
}
});
Button Back = (Button) findViewById(R.id.btn_back);
Back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Operation.this, AddProducts.class);
startActivity(intent);
}
});
Button ScanMe = (Button) findViewById(R.id.btn_scan);
ScanMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startQRScanner();
}
});
}
private void startQRScanner() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data); if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "Cancelled",Toast.LENGTH_LONG).show();
} else {
updateText(result.getContents());
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void updateText(String scanCode) {
txt_MO.setText(scanCode);
}
public class AddPro extends AsyncTask<String, String, String> {
String z = "";
Boolean isSuccess = false;
String MO = txt_MO.getText().toString();
String Material = txt_Material.getText().toString();
String Machine = txt_Machine.getText().toString();
String PFNO = txt_PFNO.getText().toString();
@Override
protected void onPreExecute() {
//pbbar.setVisibility(View.VISIBLE);
}
protected void onPostExecute(String r) {
//bbar.setVisibility(View.GONE);
Toast.makeText(Operation.this,r,Toast.LENGTH_SHORT).show();
if(isSuccess) {
// Intent i = new Intent(Operation.this, Start_Page.class);
// startActivity(i);
// finish();
}
}
protected String doInBackground(String... params) {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from Employee where Id='"+ txt_Id +"' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println(rs.getString(1));
if(rs.next())
{
txt_pic.setText(Html.fromHtml(rs.getString("Name")));
z = "Success";
isSuccess=true;
}
else
{
z = "wrong";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
}
return z;
}
}
}
我希望数据库中的值显示到另一个 Edittext 名称 txt_pic
最佳答案
您正在尝试从后台线程 doInBackground
访问 UI 组件 txt_pic
,这是不允许的。您必须从主线程设置文本。
尝试从 doInBackground
传递 rs.getString("Name")
,如下所示:
protected String doInBackground(String... params) {
String result = null;
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from Employee where Id='"+ txt_Id +"' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println(rs.getString(1));
if(rs.next())
{
result = rs.getString("Name");
z = "Success";
isSuccess=true;
}
else
{
z = "wrong";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
}
return result;
}
然后尝试从onPostExecute
进行设置。检查以下内容:
protected void onPostExecute(String r) {
//bbar.setVisibility(View.GONE);
Toast.makeText(Operation.this,z,Toast.LENGTH_SHORT).show();
if(r != null)
txt_pic.setText(Html.fromHtml(r));
if(isSuccess) {
// Intent i = new Intent(Operation.this, Start_Page.class);
// startActivity(i);
// finish();
}
}
关于java - 从 SQL Server 检索与 Android Studio 中的 Edittext 值相对应的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58833869/
现在我正在尝试实现 flash programming specification对于 PIC32MX。我正在使用 PIC32MX512L 和 PIC32MX512H。 PIC32MX512L最终必须
我是一名优秀的程序员,十分优秀!