我必須在我的android項目中上課,第一類是Main Activity,它是extend Activity,另一個類是MSSQLConnect,它是一個簡單的類,用於從數據庫獲取數據並返回它作爲數組列表如何從非活動接收信息到活動類
我用這個方法調用MSSQLConnect類,但它不工作:
MainActivity類別
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MSSQLConnect cdb = new MSSQLConnect("id", "mytable", "", "");
Toast.makeText(this, cdb.getdata("getApplicationContext()"), Toast.LENGTH_SHORT).show(); }}
MSSQLConnect類:
public class MSSQLConnect extends ContextWrapper {
private final String table;
private final String columns;
private final String whereColumn;
private final String equalsEntry;
public MSSQLConnect (Context context, String columns, String table, String whereColumn, String equalsEntry) {
super(context);
this.table = table;
this.columns = columns;
this.whereColumn = whereColumn;
this.equalsEntry = equalsEntry;
}
static String JDBC_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
static String DB_URL ="jdbc:jtds:sqlserver://xxx.xxx.x.x:xxx;DatabaseName=xxxx";
// Database credentials
static String USER = "xxxxx";
static String PASS = "xxxxx";
public ArrayList<ArrayList<String>> ourQuerryMethod() {
Connection conn = null;
Statement stmt = null;
String data[][] = null;
ArrayList<ArrayList<String>> setData = new ArrayList<ArrayList<String>>();
try {
Class.forName(getJDBC_DRIVER());
conn = DriverManager.getConnection(getDB_URL(), getUSER(), getPASS());
stmt = conn.createStatement();
String sql;
if(whereColumn.equals("") && equalsEntry.equals(""))
{
sql = "SELECT " + (columns) + " FROM " + (table);
}
else
sql = "SELECT " + (columns) + " FROM " + (table) + " WHERE "+ (whereColumn) +" = "+ (equalsEntry) +"";
ResultSet rs;
rs = stmt.executeQuery(sql);
int nc = 1;
ArrayList<String> columns = new ArrayList<String>();
int rowCount =0;
while(rs.next()) // loop rows
{
rowCount++;
if(rowCount == 1)
{
while(true){ //loop columns
try {
columns.add(rs.getString(nc));
} catch (Exception e) {
// TODO: handle exception
break;
}
nc++;
}
}
int rrowrr = 0;
for(int i=1;i<nc;i++)
{
rrowrr++;
if(rrowrr == 1)
{
setData.add(new ArrayList<String>());
}
setData.get(rs.getRow()-1).add(rs.getString(i));
}
}
rs.close();
}catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
Log.d("DBError1", se.getMessage());
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
Log.d("DBError2", e.getMessage());
} finally {
// finally block used to close resources
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {
Log.d("DBError3", se.getMessage());
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
Log.d("DBError4", se.getMessage());
} // end finally try
} // end try
return setData;
} // end main
public static String getJDBC_DRIVER() {
return JDBC_DRIVER;
}
public static void setJDBC_DRIVER(String aJDBC_DRIVER) {
JDBC_DRIVER = aJDBC_DRIVER;
}
public static String getDB_URL() {
return DB_URL;
}
public static void setDB_URL(String aDB_URL) {
DB_URL = aDB_URL;
}
public static String getUSER() {
return USER;
}
public static void setUSER(String aUSER) {
USER = aUSER;
}
public static String getPASS() {
return PASS;
}
public static void setPASS(String aPASS) {
PASS = aPASS;
}}
MSSQLConnect類和ConnectDB類是相同的。 – user1139589
然後使用上面的方法 –
複製此代碼並運行此 –