package com.hwua.template;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hwua.domain.Account;
import com.hwua.tools.DBHelper;
public class TestTemplate<T> {
public static void main(String[] args) {
List<Account> list = tt.testQuery("select * from t_account", new ResultSetHandler<List<Account>>() {
@Override
public List<Account> handler(ResultSet rSet) {
List<Account> list = new ArrayList<>();
try {
while (rSet.next()) {
Account a = new Account();
a.setAccountId(rSet.getInt("t_id"));
a.setAccountName(rSet.getString("t_name"));
a.setAccountBalance(rSet.getInt("t_money"));
list.add(a);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
});
if (list != null) {
for (Account account : list) {
System.out.println(account);
}
}
}
public void updateSQL(String sql,Object...params) {
try {
Connection conn = DBHelper.getInstance().getConnection();
conn.setAutoCommit(true);
PreparedStatement pStatement = conn.prepareStatement(sql);
int count = pStatement.getParameterMetaData().getParameterCount();
for (int i = 0; i < count; i++) {
pStatement.setObject(i+1, params[i]);
}
pStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
/***
* 通过内部接口来提供一个结果集的持有者.
* @author Administrator
*
* @param <T>
*/
interface ResultSetHandler<T>{
T handler(ResultSet rSet);
}
public T testQuery(String sql,ResultSetHandler<T> handler,Object...params) {
try {
Connection conn = DBHelper.getInstance().getConnection();
PreparedStatement pStatement = conn.prepareStatement(sql);
int count = pStatement.getParameterMetaData().getParameterCount();
for (int i = 0; i < count; i++) {
pStatement.setObject(i+1, params[i]);
}
ResultSet rSet= pStatement.executeQuery();
return handler.handler(rSet);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public View showView(){
System.out.println("请输入你的账号");
String inputid = mScanner.next();
System.out.println("请输入您的密码");
String inputpassword = mScanner.next();
System.out.println(TestTemplate.list == null);
for (Account a : TestTemplate.list) {
if(inputid .equals(a.getAccount()) && inputpassword.equals(a.getPassword())) {
return new UserView();
}else if (!(inputid.equals(a.getAccount()))){
System.out.println("账号不存在!");
return new MainView();
}else {
System.out.println("账号或密码错误!");
return new MainView();
}
}
return mView;
请问为什么我在TestTemplate中打印list数组就可以,把它声明成静态之后去LoginView调用打印进行匹配时报空指针呢 这两个不在同一个包里面
相关分类