我正在运行一个Spring MVC应用程序,由我使用JDBC访问的MySQL数据库提供支持.我一直在使用相同的代码,并且从未真正了解我是否正确使用它(正确使用连接池等).
我知道那里有JDBCTemplate,我已经考虑过使用它,但如果唯一的好处是我只是不必编写样板代码,那么我不太相信我应该使用它.实际上,我更喜欢我的代码对JDBCTemplate代码的可读性.
下面是我的DAO中的代码,出于某种原因,我觉得我没有正确使用ConnectionPooling.
public Agent get(Integer id){
ConnectionPool pool = new ConnectionPool();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
try{
String query = "SELECT * FROM agent where id= ?";
ps = connection.prepareStatement(query);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
Agent agent = null;
if(rs.next()){
agent = new Agent();
agent.setFirstName(rs.getString(1));
agent.setLastName(rs.getString(2));
agent.setStreet(rs.getString(3));
agent.setCity(rs.getString(4));
agent.setZip(rs.getString(5));
agent.setState(rs.getString(6));
agent.setUsername(rs.getString(7));
agent.setPassword(rs.getString(8));
agent.setId(rs.getInt(9));
agent.setEmail(rs.getString(10));
}
return agent;
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
finally{
ConnectionUtility utility = new ConnectionUtility();
utility.closePreparedStatement(ps);
pool.freeConnection(connection); …Run Code Online (Sandbox Code Playgroud) 我之前也发过了类似的问题.我也澄清了我的怀疑.但我还需要更多东西.将使用枚举对象作为键并将线程池实例作为值来初始化Hashmap.我很困惑如何为其他进程调用的每个对象初始化HashMap.要弄清楚:我的程序MyThreadpoolExcecutorPgm.java初始化一个HashMap我的Progran AdditionHandler.java通过传递ThreadpoolName(枚举)从HashMap请求一个线程).我收到"来自HashMap的无线程"消息.请帮帮我.
下面给出的是我的代码:
public class MyThreadpoolExcecutorPgm {
enum ThreadpoolName {
DR, BR, SV, MISCELLENEOUS;
}
private static String threadName;
private static HashMap<ThreadpoolName, ThreadPoolExecutor>
threadpoolExecutorHash;
public MyThreadpoolExcecutorPgm(String p_threadName) {
threadName = p_threadName;
}
public static void fillthreadpoolExecutorHash() {
int poolsize = 3;
int maxpoolsize = 3;
long keepAliveTime = 10;
ThreadPoolExecutor tp = null;
threadpoolExecutorHash = new HashMap<ThreadpoolName, ThreadPoolExecutor>();
for (ThreadpoolName poolName : ThreadpoolName.) // failing to implement
{
tp = new ThreadPoolExecutor(poolsize, maxpoolsize, keepAliveTime,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
threadpoolExecutorHash.put(poolName, tp);
} …Run Code Online (Sandbox Code Playgroud) 我尝试使用模板化派生类实现访问者模式
这里是VisitorTemplate.hpp,我专门在类Visitor中派生,但我希望能够处理任何类型:
编辑:感谢interjay的建议,代码现在编译并运行没有错误
#ifndef VISITORTEMPLATE_HPP_
#define VISITORTEMPLATE_HPP_
#include <iostream>
#include <string>
using namespace std;
template<class T> Derived;
class Visitor
{
public:
virtual void visit(Derived<string> *e) = 0;
};
class Base
{
public:
virtual void accept(class Visitor *v) = 0;
};
template<class T>
Derived: public Base
{
public:
virtual void accept(Visitor *v)
{
v->visit(this);
}
string display(T arg)
{
string s = "This is : " + to_string(arg);
return s;
}
};
class UpVisitor: public Visitor
{
virtual void …Run Code Online (Sandbox Code Playgroud) 我试图运行它但它崩溃时它到达getDefaultSharedPreferences().为什么?
这是首选项活动.IntentService当它被摧毁时它会发射
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class CCTDetectorActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
private SharedPreferences settings;
static public String nameOfFile = "name_of_file";
static public String nameOfFileDefaultValue = "detected_f.xml";
static public String portNumber = "port_number";
static public String portNumberDefaultValue = "25015";
static public String keepAlive = "keep_alive";
static public String keepAliveDefaultValue = "3";
static public String nameOfSettings = "settings";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); …Run Code Online (Sandbox Code Playgroud) 我有一个使用自定义Connection类(扩展线程)到硬件控制器的服务.当用户更喜欢时,我希望永久保持这种联系.当Android设备失去互联网连接,在Wi-Fi之间切换等时,我已经有了处理代码.
为了保持连接,控制器要求您在每5分钟内与其通话.我目前在Connection类中启动一个在while()中运行的线程,并检查系统时间和上次通信的时间,当> 4分钟时它会请求状态.由于某种原因,在不同时间通信不会及时发生.即,在5分钟后发生.据我所知,服务不会死,但控制器的"Ping"迟到了.当我将手机插入充电器(或调试器)时,不会发生这种情况.此外,当我将服务移动到前台时,行为是相同的.
手机进入睡眠状态时手机是否会减速?
有没有更好的办法?
我认为它是AlarmManger,但是我无法在服务中使用内部类.我尝试使用API演示作为起点,但我似乎无法弄清楚如何注册广播接收器.我试图以编程方式注册接收器,而不更改清单.
public class DeviceConnectionService extends Service {
@Override
public void onCreate() {
Intent intent = new Intent(this, PingConnection.class);
intent.setAction("KEEP_CONNECTION_ALIVE");
PendingIntent sender = PendingIntent.getBroadcast(this,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 15*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 15*1000, sender);
// register to listen to the Alarm Manager
if (mPingConnectionReceiver == null) {
mPingConnectionReceiver = new PingConnection(); …Run Code Online (Sandbox Code Playgroud) 好吧基本上我想开始从a听一个更新BroadcastReceiver,并且由于某种原因,我的访问禁用了所有提供者.这是代码片段
LocationManager locationManager = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
String majorProvider = locationManager.getBestProvider(criteria, true);
Log.d("location test", "major provider " + majorProvider);
List<String> allProviders = locationManager.getAllProviders();
for(int i=0; i < allProviders.size();i++) {
Log.d("location test", "provider " + i + ": " + allProviders.get(i));
}
Log.d("location test", "enabled...");
allProviders = locationManager.getProviders(true);
for(int i=0; i < allProviders.size();i++) {
Log.d("location test", "enabled provider " + i + ": " + allProviders.get(i));
}
Run Code Online (Sandbox Code Playgroud)
和我的logcat结果
04-09 11:23:35.763: D/location test(32576): major provider …Run Code Online (Sandbox Code Playgroud) 当我尝试在1.5中编译我以前的java 1.4代码时,我得到了这个泛型绑定不匹配问题.代码如下
try {
ArrayList simplePrincipals = new ArrayList(
((java.util.Collection) (subject.getPrincipals(Class
.forName("com.efunds.security.jaas.SimplePrincipal")))));
if (simplePrincipals.size() > 0) {
((SimplePrincipal) simplePrincipals.get(0))
.setPermissions(webPerm);
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
绑定不匹配:
getPrincipals(Class<T>)类型Subject 的泛型方法不适用于参数(Class<capture#1- of ?>).推断类型capture#1-of ?不是有界参数的有效替代<T extends Principal>
我是Qt编程的新手.我得到发出信号的悬念,并用一个插槽捕捉信号,但是可以将插槽作为常规方法调用吗?
例如,在以下代码段中:
class someClass{
..
..
//method
void emitsig1(int val)
{
emit sig1(val);
}
public signals:
void sig1(int a);
};
class someOtherClass{
..
..
public slots:
int onSig1(int a)
{
//some computation on a
return a;
}
};
int main(argc, char** argv){
..
..
someClass obj1 = new someClass();
someOtherClass obj2 = new someOtherClass();
int value = 10, result =0;
obj1.emitsig1(value);
QObject::connect(obj1, SIGNAL(sig1(int), obj2, SLOT(onSig1(int)));
int newvalue = 100;
//is it legal to do this, and if so, what value …Run Code Online (Sandbox Code Playgroud) 经过1000多private秒后private,我发现可能不需要它
public class Outer {
private static class Inner { // you may drop static
private void innerMethod() {}
}
}
Run Code Online (Sandbox Code Playgroud)
是否有任何情况下,该下降private从innerMethod()将使在差异封装(或使用,由外例如)?想想反思
如果不是,建议放弃它或保持它与编码风格?
我说没有和下降,但不知道真的.
编辑:刚刚意识到,这样我做到这一点肯定是错误的-至少对于Inner的领域 -宣称这些领域的私人然后在外部类中使用它们-因为这会产生('合成’)访问的字节码-这是在最臃肿.太棒了 - 所以现在我更感兴趣的是有一个关于声明这些(Inner的字段,使用的方法Outer)包私有(或公共@JBNizet在评论中说)的安全含义的帐户
如何在 Eclipse 中添加以下内容?
我在 Eclipse 中创建了一个 Android 应用程序项目,并使用 eGit 和 BitBucket 将其置于版本控制之下(我再次不知道这是否是描述它的最佳方式,因为我是版本控制新手)。到目前为止,我已经编写了一些代码并进行了多次提交。我邀请了一位加入该项目的朋友。昨晚他们更改了一些代码。
今天,我启动了 Eclipse,想要继续该项目的工作,但想从他们停止的地方开始。我右键单击该项目并转到“团队”->“同步”(只是因为这对我来说似乎是最合乎逻辑的步骤)。一切都没有如我担心的那样发生。
如何将最新版本的工作代码下载到本地计算机上?当我完成工作时,我是否只需像我一直在做的那样通过右键单击项目然后转到:团队 -> 提交,然后团队 -> 推送到上游来提交?
java ×5
android ×4
alarmmanager ×1
bitbucket ×1
c++ ×1
c++11 ×1
coding-style ×1
collections ×1
crash ×1
eclipse ×1
egit ×1
generics ×1
git ×1
jdbc ×1
mysql ×1
preferences ×1
qt ×1
reflection ×1
security ×1
service ×1
signals ×1
slot ×1
templates ×1