第一次建立连接时,我能够从我的数据库中检索信息,但每当我尝试重新连接到数据库并从不同类的数据库中检索更多信息时,它都不起作用。我正在尝试将从数据库中检索的信息放入 Android Studio 中的 TextViews,但它们一直显示为“null”。我将向您展示我第一次连接到数据库时的代码、我的连接管理器类,以及下次我尝试连接到数据库时的代码。我认为这可能是因为连接未正确关闭,但我确保关闭了结果集、语句和连接。
这是我即将到期的高级项目,如有任何帮助,我们将不胜感激。谢谢!
这是我第一次连接到数据库的代码:
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
public final String mEmail;
private final String mPassword;
UserLoginTask(String email, String password) {
mEmail = email;
mPassword = password;
}
@Override
protected Boolean doInBackground(Void... params) {
try {
Connection conn = ConnectionManager.getConnection();
Statement st = null;
if (conn != null)
st = conn.createStatement();
ResultSet resultSet = null;
if (st != null)
resultSet = st.executeQuery("SELECT * FROM userinfo WHERE email='" + mEmail + "'");
int counter = 0;
if (resultSet != null)
while (resultSet.next()) {
counter++;
try {
if (mPassword.equals(resultSet.getString("password"))) {
UserLoginInfo.userEmail = mEmail;
UserLoginInfo.fName = resultSet.getString("firstname");
UserLoginInfo.lName = resultSet.getString("lastname");
st.close();
resultSet.close();
conn.close();
return (true);
} else
return (false);
} catch (SQLException e3) {
e3.printStackTrace();
}
}
if (counter == 0)
return false;
// TODO: register the new account here.
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute ( final Boolean success){
mAuthTask = null;
showProgress(false);
if (success) {
finish();
startActivity(new Intent("NavDrawer"));
} else {
mPasswordView.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}
@Override
protected void onCancelled () {
mAuthTask = null;
showProgress(false);
}
}
这是我的连接管理器类:
package com.capstone.hammond.wallstreetfantasyleaguefinal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ConnectionManager {
private static String url = "jdbc:oracle:thin:@141.216.24.31:1521:fsdb";
private static String driverName = "oracle.jdbc.driver.OracleDriver";
private static String username = "xxxx";
private static String password = "xxxx";
private static Connection con;
public static Connection getConnection() {
try {
Class.forName(driverName);
try {
if(con !=null)
return con;
else
return con = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
// log an exception. for example:
System.out.println("Failed to create the database connection.");
}
} catch (ClassNotFoundException ex) {
// log an exception. for example:
System.out.println("Driver not found.");
}
return con;
}
}
这是我第二次尝试连接到数据库:
package com.capstone.hammond.wallstreetfantasyleaguefinal;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class WeeklyMatchFragment extends Fragment {
TextView user1;
TextView user2;
TextView bank1;
TextView bank2;
String oppFName;
String oppLName;
float oppBank;
View rootview;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.fragment_weekly_match, container, false);
return rootview;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new MatchDetails();
user1 = (TextView) view.findViewById(R.id.user1);
user1.setText(UserLoginInfo.fName + " " + UserLoginInfo.lName);
bank1 = (TextView) view.findViewById(R.id.bank1);
bank1.setText("Bank: " + UserLoginInfo.bank);
user2 = (TextView) view.findViewById(R.id.user2);
user2.setText(oppFName + " " + oppLName);
bank2 = (TextView) view.findViewById(R.id.bank2);
bank2.setText("Bank: " + oppBank);
}
public class MatchDetails extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Connection conn = ConnectionManager.getConnection();
ResultSet rs = null;
Statement st = null;
if (conn != null)
st = conn.createStatement();
if (st != null)
rs = st.executeQuery("SELECT * FROM L1_Standings WHERE EMAIL = '" + UserLoginInfo.userEmail + "'");
if (rs != null)
while (rs.next()) {
try {
UserLoginInfo.bank = rs.getInt("BANK");
UserLoginInfo.currOpp = rs.getInt("CURR_OPP");
} catch (SQLException e3) {
e3.printStackTrace();
}
}
Statement st1 = null;
if (conn != null)
st1 = conn.createStatement();
ResultSet rs1 = null;
if (st != null)
rs1 = st1.executeQuery("SELECT * FROM USERINFO WHERE EMAIL = '" + UserLoginInfo.currOpp + "'");
if (rs1 != null)
while (rs1.next()) {
try {
oppFName = rs1.getString("FIRSTNAME");
oppLName = rs1.getString("LASTNAME");
} catch (SQLException e3) {
e3.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
}
最佳答案
您应该在 finally block 中关闭 JDBC 连接。
如果您使用的是 JAVA 7,请使用 try with resource 语句 - try-with-Resource ;
关于java - 您如何正确关闭 JDBC 连接以供重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787233/