所以我运行这个:
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);
}
}
}
这是我调用DB并得到结果的代码:
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项目中结果是正确的,而在另一个Eclipse项目中结果是NULL - >我写了3次null。这是一个错误还是我遗漏了一些非常重要的东西?
请求任何帮助。