在sql server上使用jdbc PreparedStatement获取查询计划

时间:2016-07-04 15:11:17

标签: java sql-server jdbc prepared-statement

使用Statment,resultSet.getObject将查询计划返回为xml

 Connection conn = getConnection();
        String query = " SET SHOWPLAN_XML on ";
        Statement st = conn.createStatement();
        boolean execute=st.execute(query);
        log.info("execute status {} " , execute);
         query = " SELECT ATMPROFILES.TERMID as COLUMNID, ATMPROFILES.TERMID as COLUMNNAME FROM ATMPROFILES (NOLOCK) "
                + " WHERE Authprocessname = 'ATMST' "
                + "ORDER BY ATMPROFILES.TERMID ";
        ResultSet rs = st.executeQuery(query);
        while(rs.next())
        {
            Object object = rs.getObject(1);
            log.info("Query Plan {}  ", object);
        }

但是如果我通过PreparedStatement执行相同的操作,它将返回实际结果而不是QueryPlan

 Connection conn = getConnection();
        String query = " SET SHOWPLAN_XML on ";
        PreparedStatement ps = conn.prepareStatement(query);
        boolean execute = ps.execute();
        log.info("execute status {} " , execute);

         query = " SELECT ATMPROFILES.TERMID as COLUMNID, ATMPROFILES.TERMID as COLUMNNAME FROM ATMPROFILES (NOLOCK) "
                + " WHERE Authprocessname = 'ATMST' "
                + "ORDER BY ATMPROFILES.TERMID ";
        ps=conn.prepareStatement(query);
        execute=ps.execute();
        log.info("execute status {} " , execute);
        ResultSet rs = ps.getResultSet();
        while(rs.next())
        {
            Object object = rs.getObject(1);
             // here it returns selected object
            log.info("Query Plan {}  ", object); 

        }

有任何想法通过PreparedStatement实现这一目标。

1 个答案:

答案 0 :(得分:0)

我还没有找到任何参考,为什么执行class Employee(models.Model): session = models.ForeignKey( 'sessions.Session', verbose_name='Session', blank=True, null=True, ) email = models.CharField(max_length=255) password1 = forms.CharField(max_length=30, widget=forms.PasswordInput()) #render_value=False password2 = forms.CharField(max_length=30, widget=forms.PasswordInput()) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) region = models.ForeignKey(Region, on_delete=models.CASCADE) workgroup = models.ForeignKey(Workgroups, on_delete=models.CASCADE) user_since = models.DateTimeField('Signed up since', default=timezone.now()) rights = models.IntegerField(default=0) def __str__(self): return self.email def clean_username(self): # check if username dos not exist before try: User.objects.get(username=self.cleaned_data['email']) #get user from user model except User.DoesNotExist : return self.cleaned_data['email'] raise ValidationError("this user exist already") def clean(self): # check if password 1 and password2 match each other if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:#check if both pass first validation if self.cleaned_data['password1'] != self.cleaned_data['password2']: # check if they match each other raise ValidationError("passwords dont match each other") return self.cleaned_data def dosave(self): # create new user new_user=User.objects.create_user(self.cleaned_data['username'], self.cleaned_data['email'], self.cleaned_data['password1']) new_user.save() return new_user 作为准备好的声明是行不通的;但是,当您直接运行此语句并将实际查询作为预准备语句时,您应该获得所需的结果。在代码中:

myuser = Employee.objects.create(first_name='Annie',
                                     last_name='Levers', email='annielevers@test.com',
                                     region=mymun, workgroup=mygroup, user_since=timezone.now())
    self.assertEquals(
        str(myuser),
        'annielevers@test.com',
    )

    # Check user.auth db
    userpk = myuser.id
    user = get_object_or_404(User, pk=userpk)
    user.set_password('abcxyz123')
    user.save()

希望有所帮助。