从android中的sql server下载表格行时显示进度条
Posted
技术标签:
【中文标题】从android中的sql server下载表格行时显示进度条【英文标题】:show progress-bar while downloading table row from sql server in android 【发布时间】:2017-05-22 00:26:06 【问题描述】:我正在从 android 中的 sql server 获取表行,我想在进度条中显示,但我遇到了问题。我正在使用 SQL Connection 类,它不允许占用表行的长度。我的连接类。
String ip="192.168.43.85";
String classs="net.sourceforge.jtds.jdbc.Driver";
String db="mydatabasename";
String uname="myname";
String pass="pass";
@SuppressLint("New Api")
public Connection CON() throws IOException
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection=null;
String ConUrl=null;
try
Class.forName(classs);
ConUrl="jdbc:jtds:sqlserver://"+ip+";"
+ "databaseName="+db+";user="+uname+";password="
+pass+";";
connection= DriverManager.getConnection(ConUrl);
catch (ClassNotFoundException e)
Log.e("ERRO",e.getMessage());
catch (SQLException e)
Log.e("ERROR",e.getMessage());
catch (Exception e)
Log.e("ERROr", e.getMessage());
return connection;
我尝试了一些来自 google 的示例,但所有示例都使用 http 和 url,但我没有 url 来查找长度。
Connection conn = null;
try
conn = connectionClass.CON();
catch (IOException e)
e.printStackTrace();
if (conn == null)
Toast.makeText(getApplicationContext(), "Error in Connection with server.Make sure your internet connection is ON", Toast.LENGTH_SHORT).show();
else
Statement stmt = null;
try
//String Slqquery = "select * from Zmat";
String Slqquery1 = "select GlobalSummaryReportFlag,StudyID,StoreCode,StudyDesc from GlobalCountSummaryReport where GlobalSummaryReportFlag='1' and StudyId='" + strStudyId1 + "' and StoreCode='" + strStudyCode2 + "' and StudyDesc='" + strStudyDes3 + "'";
stmt = conn.createStatement();
ResultSet rs3;
rs3 = stmt.executeQuery(Slqquery1);
if (rs3.next())
progress1=new ProgressDialog(MenuActivity.this);
progress1.setMessage("Downloading GC");
progress1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress1.setIndeterminate(true);
progress1.setProgress(0);
progress1.setMax(100);
progress1.show();
final int totalProgressTime = 100;
new Thread(new Runnable()
@Override
public void run()
int jumpTime = 0;
while(jumpTime < totalProgressTime)
try
Data();
if (countForHireMapping == 0 || CountForArticle == 0)
Toast.makeText(getApplicationContext(), "Downloaded UnSuccessfully Please Retry", Toast.LENGTH_SHORT).show();
delete2();
progress1.dismiss();
else
sleep(200);
jumpTime += 5;
progress1.setProgress(jumpTime);
progress1.dismiss();
Intent intent = new Intent(MenuActivity.this, LocationActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Downloaded Successfully", Toast.LENGTH_SHORT).show();
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
).start();
else
Toast.makeText(getApplicationContext(), "Gc Not Completed", Toast.LENGTH_SHORT).show();
catch (SQLException e)
e.printStackTrace();
`
【问题讨论】:
【参考方案1】:您应该在 MainActvity 中使用 AsyncTask
它包含3个方法:
onPreExecute():在这个方法中放置你想要在类执行之前做的任何事情。(在你的例子中是一个 progressBar )
doInBackground():在此方法中放置您希望应用在后台执行的任何操作。(例如:从服务器获取数据)
onPostExecute():在此方法中,在后台进程完成后放置您想要执行的任何操作。 (示例:根据从在线服务器接收到的数据设置视图)
Here is a tutorial 让您快速了解 AsyncTask。
编码愉快。
【讨论】:
以上是关于从android中的sql server下载表格行时显示进度条的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server 2008数据库中的一个表格的数据行有没限制,最多多少行