|
|
|
Andorid connect dabase error 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference |
|
|
|
|
|
|
|
Code (C#)
package com.example.boonmapi.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
Button EXECUTAR;
EditText ValorBusca;
ListView Lista;
Connection connect;
SimpleAdapter AD;
private void declarar(){
EXECUTAR = (Button) findViewById(R.id.btn_buscar);
ValorBusca =(EditText)findViewById(R.id.txt_buscar);
Lista =(ListView)findViewById(R.id.list_output);
}
private void inicializer() {
declarar();
ValorBusca.setText("SELECT TOP 10 * FROM ES_LOG");
connect = CONN("boonmapi","031981209","DB_MAEJOPHRAE","PAKPOOM\\SQLEXPRESS");
}
@SuppressLint("NewApi")
public Connection CONN (String user,String pass,String DB,String server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnURL = "jdbc:jtds:sqlserver://" + server + ";" + "databaseName=" + DB + ";user=" + user + ";password=" + pass + ";"+ "ssl=false";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
}
return conn;
}
public void QuerySQL(String COMANDOSQL){
try {
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery(COMANDOSQL);
List<Map<String,String>> data = null;
data =new ArrayList<Map<String, String>>();
while (rs.next()){
Map<String,String> datanum = new HashMap<String, String>();
datanum.put("A", rs.getString("USERNAME"));
datanum.put("B", rs.getString("DATA"));
data.add(datanum);
System.out.println( rs.getString("DATA"));
}
String[] from = {"A","B"};
int[] views={R.id.txt_titulo,R.id.txt_conteudo};
AD = new SimpleAdapter(this,data,R.layout.modelo,from,views);
Lista.setAdapter(AD);
}catch (Exception e){
Log.e("ERRO",e.getMessage());
System.out.println(COMANDOSQL );
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inicializer();
EXECUTAR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
QuerySQL(ValorBusca.getText().toString());
}
});
}
}
Tag : Mobile, Ms SQL Server 2008, Android, Windows
|
|
|
|
|
|
Date :
2016-04-28 15:34:53 |
By :
boonmapi |
View :
1202 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|