2016-01-29 76 views
3

我是新來的java和我需要幫助添加我的數據庫值爲多警報(桌面不是Android)。這是我爲報警類別代碼:爲多路報警添加數據庫值

public class Alarm { 
Thread t; int str = 0; 
Connection conn = null ; 

public void checkAlarm(int d,int m,int hh,int mm,final String ampm,final String message){ 
final int a=hh,b=m,c=d,f=mm;     
if(ampm.equals("AM")) 
{ str=0; } 
else if(ampm.equals("PM")) 
{ 
str=1; 
} 
final boolean whileloop=true; 
t=new Thread(){ 
     private Object ac; 
public void run() 
{ 
    while(whileloop==true) 
{ 
    Calendar d = Calendar.getInstance(); 
    //DATE 
    int date1=d.get(Calendar.DATE); 
    int month=d.get(Calendar.MONTH); 
    //TIME 
    int hours=d.get(Calendar.HOUR); 
    int mins=d.get(Calendar.MINUTE); 
    int str1=d.get(Calendar.AM_PM); 
    if(c==date1&&f==month&&a==hours&&b==mins&&st­r==str1) 
    { 
     FrameNotification a = new FrameNotification();  
     a.setVisible(true);  
     break;     
    } 
} 
} }; 

t.setPriority(Thread.MIN_PRIORITY); 
t.start(); 
} 

我嘗試這樣做,它的工作(不包含數據庫值)是這樣的:

 Alarm a=new Alarm(); 
     a.checkAlarm(29,((1)-1),3,19,"PM","X­D"); 

問題是我不知道如何設置

a.checkAlarm(29,((1)-1),3,19,"PM","X­D"); 

a.checkAlarm(//insert database value here?); 

回答

0
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Connection; 
import org.apache.commons.lang.builder.ToStringBuilder; 
import org.apache.commons.lang.builder.ToStringStyle; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
public class RuleDBConnection { 

private static Logger logger = LoggerFactory.getLogger(RuleDBConnection.class); 

public static class DBInfo { 
    String host; 
    int port; 
    String database; 
    String user; 
    String password; 
    String table; 
    String queueName; 
    String timeZoneID; 
    int fetchSize; 
    DBType dbType = DBType.MYSQL; 

    public String getHost() { 
     return host; 
    } 

    public void setHost(String host) { 
     this.host = host; 
    } 

    public int getPort() { 
     return port; 
    } 

    public void setPort(int port) { 
     this.port = port; 
    } 

    public String getDatabase() { 
     return database; 
    } 

    public void setDatabase(String database) { 
     this.database = database; 
    } 

    public String getUser() { 
     return user; 
    } 

    public void setUser(String user) { 
     this.user = user; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public DBType getDbType() { 
     return dbType; 
    } 

    public void setDbType(DBType dbType) { 
     this.dbType = dbType; 
    } 

    @Override 
    public String toString() { 
     return ToStringBuilder.reflectionToString(this, 
       ToStringStyle.SHORT_PREFIX_STYLE); 
    } 

    public static enum DBType { 
     ORACLE, MYSQL 
    } 
} 

public static Connection getConnection(DBInfo dbInfo) throws SQLException { 
    try { 
     String url = null; 
     if (dbInfo.getDbType() == DBInfo.DBType.MYSQL) { 
      Class.forName("com.mysql.jdbc.Driver"); 
      url = String.format("jdbc:mysql://%s:%d/%s", dbInfo.getHost(), 
        dbInfo.getPort(), dbInfo.getDatabase()); 
     } else { 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      url = String.format("jdbc:oracle:thin:@%s:%d:%s", dbInfo.host, 
        dbInfo.port, dbInfo.database); 
     } 
     logger.info("url=>{}", url); 
     return DriverManager.getConnection(url, dbInfo.user, 
       dbInfo.password); 
    } catch (ClassNotFoundException e) { 
     throw new RuntimeException(e); 
    } 
} 

}

+0

使用此代碼段,你可以直接得到連接數據庫,並使用MySQL的正常查詢。 – Boola

+0

我一直在改變功能爲無效,所以它現在運作良好,非常感謝你回答我的問題,我很感激它:) – amel