首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >值不会转储到表temperature_demo中

值不会转储到表temperature_demo中
EN

Stack Overflow用户
提问于 2014-03-27 18:51:58
回答 1查看 44关注 0票数 0

我编写了这段代码,以便从表中删除重复的条目,然后将其插入到另一个表中。代码会执行,但值不会更新到表中。Query4没有被执行。任何建议都会很有帮助。谢谢。

代码语言:javascript
复制
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class duplicate
{
    public static void main(String args[])
    {
        Statement stat=null,stat1=null,stat2=null;
        Connection con=null;
        ResultSet result1,result2;
        int s_id=0;
        String date=null,time=null,temp=null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Loaded Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost:3306/preprocessor","root","bigdata");
            System.out.println("Connected to mysql");
            stat=con.createStatement();
            stat1=con.createStatement();
            stat2=con.createStatement();
            String query1="insert into temperature_demo(ddslno,ddtstation_id,ddtdate,ddttime,ddtemp,ddtype_code,ddelevation) select dslno,dtstation_id,dtdate,dttime,dtemp,dtype_code,delevation from temperature_dup where dslno=1";
            String query2="select * from temperature_demo";
            String query3="select * from temperature_dup";
            stat1.execute(query1);
            result1=stat1.executeQuery(query2);
            result2=stat2.executeQuery(query3);

                while(result1.next())
                {
                    s_id=result1.getInt(2);
                    date=result1.getString(3);
                    time=result1.getString(4);
                    temp=result1.getString(5);
                    break;
                }
                while(result2.next())
                {
                    int sno=result2.getInt(1);
                    int s1_id=result2.getInt(2);
                    String date1=result2.getString(3);
                    String time1=result2.getString(4);
                    String temp1=result2.getString(5);
                    String type_cd=result2.getString(6);
                    String elev=result2.getString(7);
                    String query4="insert into temperature_demo values(sno,s1_id,'date1','time1','temp1','type_cd','elev')";
                    try
                    {
                    if( (s_id==s1_id)&&(date.equals(date1))&&(time.equals(time1))&&(temp.equals(temp1)) )
                        ;
                    else
                    {
                        System.out.println(sno+" "+s1_id+" "+date1);
                        stat1.execute(query4);
                    }
                    }
                    catch(Exception ex)
                    {

                    }
                }

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if(con!=null)
                try
                {
                    con.close();
                }
                catch(Exception ex)
                {

                }
            if(stat1!=null)
                try
                {
                    stat1.close();
                }
                catch(Exception ex)
                {

                }
            if(stat2!=null)
                try
                {
                    stat2.close();
                }
                catch(Exception ex)
                {

                }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-03-28 16:21:23

我进行了更改,结果如下所示。PreparedStatement pstat;pstat=con.prepareStatement("INSERT INTO temperature_demo VALUES(?,?)");while(PreparedStatement 2.next()){ int s1_id=result2.getInt(1);int date1=result2.getString(2);String date1=result2.getString(3);String time1=result2.getString(4);String temp1=result2.getString(5);String type_cd=result2.getString(6);String elev=result2.getString(7);

代码语言:javascript
复制
                    try
                    {
                    if( (s_id==s1_id)&&(date.equals(date1))&&(time.equals(time1))&&(temp.equals(temp1)) )
                        ;
                    else
                    {
                        System.out.println(sno+" "+s1_id+" "+date1);
                        pstat.setInt(1,sno);
                        pstat.setInt(2,s1_id);
                        pstat.setString(3,date1);
                        pstat.setString(4,time1);
                        pstat.setString(5,temp1);
                        pstat.setString(6,type_cd);
                        pstat.setString(7,elev);
                    }

else块中的第一条语句似乎执行得很好。以下pstat语句(pstat是一个preparedStatement变量)没有执行

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22685382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档