我真的有一个奇怪的奇怪情况。这让我快疯了。我玩了 1 个小时的代码,最后我发现它在另一个项目中应该可以正常工作。我想从数据库中获取一些信息。
所以我运行这个:
public class DBtest {
public static void main(String[] args) throws Exception {
KnockKnockProtocol kkp = new KnockKnockProtocol();
kkp.processInput("USERNAME,PASSWORD");
}
}
这称为:
import java.net.*;
import java.io.*;
import org.apache.commons.codec.binary.Base64;
import de.vogella.mysql.first.load_login;
public class KnockKnockProtocol {
public String theOutput[];
public void processInput(String theInput) throws Exception {
String[] data = theInput.split(",");
byte[] encoded = Base64.encodeBase64(data[1].getBytes());
String pass = new String(encoded);
load_login get = new load_login();
theOutput = get.nalozi(data[0], pass);
for (String n : theOutput) {
System.out.println(n);
}
}
}
这是我调用数据库并获得结果的代码:
package de.vogella.mysql.first;
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.util.Date;
import org.apache.commons.codec.binary.Base64;
public class load_login {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
int count = 0;
String user, password, ssl, tls, smtp_domain, smtp_port, signature = null;
String[] rezultat = new String[3];
public String[] nalozi(String mail, String pass) throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/?????"
+ "user=?????&password=?????");
String checkSql = "select * from OFFLINE where (user_mail='" + mail
+ "' AND user_pass='" + pass + "')";
Statement st = connect.createStatement();
resultSet = st.executeQuery(checkSql);
while (resultSet.next()) {
rezultat[0] = user = resultSet.getString("user_mail");
rezultat[1] = password = resultSet.getString("user_pass");
rezultat[2] = ssl = resultSet.getString("ssl_protocol");
}
} catch (Exception e) {
} finally {
close();
}
return rezultat;
}
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
对不起所有这些代码。所以关键是,在一个 Eclipse 项目中,结果是正确的,而在另一个项目中,结果是 NULL -> 我得到了 3 次写入 null。这是一个错误还是我错过了一些非常重要的东西?
感谢您的帮助。