JAVA sql语句动态参数问题

对sql语句设置动态参数

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class sqlTest {
    static String url = "jdbc:oscar://192.168.1.208:2004/OSRDB";
    static String username = "SYSDBA";
    static String password = "szoscar55";
    public static void main(String[] args) throws Exception{
        Class.forName("com.oscar.Driver");
        Connection con = DriverManager.getConnection(url, username, password);
        DatabaseMetaData data = con.getMetaData();        
//        String sql = "select * from SINAWEIBO_500 where USER_ID = ?";
        PreparedStatement st = con.prepareStatement("select * from SINAWEIBO_500 where MESSAGE_TYPE = ?");
        st.setObject(1, "1");        
        ResultSet rs = st.executeQuery();
        while(rs.next()){
            System.out.println(rs.getString(2));
        }
    }

}

刚开始,写的prepareStatement(sql);

st.setObject(1,"1")结果报的错是Dynamic parameter must used only in prepare statement,后来把sql语句移到preparestatement中,就运行成功了,我也不清楚为什么把sql语句提出来就不对了

posted on 2013-08-15 13:50  ywf—java  阅读(1379)  评论(0编辑  收藏  举报

导航