java的类中
String id="200905232323";
String a="id";
我想通过a获取到id的值。也不知道大家能明白的我的意思没。
就是我知道一个变量的值id为“200905232323”,但是我设了一个变量a,通过字符串处理我能知道a为id,我现在是想获得“200905232323”,但是不能直接用id获取
各位大虾帮忙!

解决方案 »

  1.   

    String id="200905232323"; 
    String a=id; System.out.print("真的不清楚你想做什么...");
      

  2.   


    Java codeString id="200905232323"; 
    String a=id; 
    System.out.print("你在干什么啊??");
      

  3.   

    你好像知道了你的意思,String  id="200905232323";String s="id";你是不是想用String字符串"id"代替变量id使用?
    可是一个id是变量,另一个却是字符串。
      

  4.   

    不大理解如果你是说按照这样来做
    String id="200905232323"; 
    String a="id"; 
    a肯定得不到id的值。因为两个字符串的值不一样。。如果你是说 你不能直接访问id  想通过a来取得id的值的话,那么id应该是一个有限制的变量吧,否则你这样访问的毫无意义啊,
    String id="200905232323"; 
    String a=id; 你可以这样写,但是我觉得意义不大 因为,其实 id和a的值是一样的,,另外 实在不知道 你说的不能直接访问id。是什么意思。。 
      

  5.   

    “但是我设了一个变量a,通过字符串处理我能知道a为id”,你这话说的不对啊,不能这么整
      

  6.   


    public class Test
    {
        public String id="200905232323";
        public String a="id";
        public static void main(String[] args)
        {
            try
            {
                Test t = new Test();
                java.lang.reflect.Field f = t.getClass().getDeclaredField(t.a);
                System.out.println((String)f.get(t));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
      

  7.   

    楼主是需要这个结果吧,^_^,java的反射可以帮助我们解决很多看似复杂的问题
      

  8.   

    String id="111"; 
    String username="careers"; 
    String userpassword="111111"; 
    String sql="insert into Person(id,username,password) values ('"+id+"','"+username+"','"+userpassword+"')"; 我的意思是要组装出这样一个sql语句,但是我不想通过字符串拼接的方法,因为用到的地方很多。
    我通过正则表达式能把INSERT INTO [dbo].[JobSeekerInfo] 
              ([ID] 
              ,[UserName] 
              ,[UserPassword]) 
        VALUES 
              ({@id} 
              ,{@userName} 
              ,{@userPassword})中需要替换为值的变量名id,userName,userPassword取出来。但是我要把{@id}替换为‘111’...现在大家明白我的意思了吧!
    9楼的意思是对的。但是 public String a="id";必须定义为全局的吗?这个值我必须通过传递过来啊!这个方法没用过,所以恳请您能还能再细致一点,万分谢谢!
      

  9.   

    我只是希望通过这个例子能够给你一些思路,具体的实现还要靠你自己,其他的功能看看java.lang.reflect这个包的文档吧
      

  10.   

        public String editKnowledge(String title, String content, String id) {
            String sql = "UPDATE [dbo].[jobKnowledge] SET [title] = {@title} ,[content] = {@content} ,[lastUpdate] = getDate() WHERE [ID] = {@id}";
            //todo:将{@title}替换为title的值;将{@content}替换为content的值;将{@id}替换为id的值。通过正则表达式"\\{\\@(\\w)+\\}"可以匹配出其中的{@title};{@content};{@id}";
            return sql;
        }
    也不知道表述清楚了没有,做毕业设计,用到的地方很多,所以不想通过拼接的方法实现。希望大虾们能指点一哈,万分感谢!
      

  11.   

    java.sql.PreparedStatement
    用过没有?
      

  12.   

    public boolean checkUserName(String sql,String userName, String password) {
            Connection con = null;
            Boolean bool=false;
            try {
                Class.forName(driver);
                con = DriverManager.getConnection(url);
                PreparedStatement pstmt;
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, userName);
                pstmt.setString(2, password);
                ResultSet rs = pstmt.executeQuery();
                bool = rs.next();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SQLServer.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            } catch (SQLException ex) {
                Logger.getLogger(SQLServer.class.getName()).log(Level.SEVERE, null, ex);
                return false;
            }
            return bool;
        }
    这个用过啊!我知道了,可以通过这种方法实现!呵呵,谢谢楼上的。但是用我想的那种也应该能够实现吧!
      

  13.   

    给你一个比较简单的例子看看能不能用上    public static void update()
        {
            Connection con = ConnectionManager.getConnection();
            Statement st = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try
            {
                ps = con.prepareStatement(“update tbl_user set name=?,address=?,age=? where id=?”);
                ps.setString(1,"username");
                ps.setString(2,"你住在哪里啊");
                ps.setInt(3,55);
                ps.setLong(4,"1001");
                //ps.setTimestamp(9,new Timestamp(ng.getDT_EXPIRATION().getTime()));
                ps.executeUpdate();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                //关闭连接
            }    }
      

  14.   

    哦,[email protected]或者[email protected]谢谢你啊!
      

  15.   

    楼主是不是想做一个映射,类似Map的东西,把id设置成"200905232323"啊?
      

  16.   

    我在14楼详细的表述了我的意思,只要能实现功能就好。    public String editKnowledge(String title, String content, String id) { 
            String sql = "UPDATE [dbo].[jobKnowledge] SET [title] = {@title} ,[content] = {@content} ,[lastUpdate] = getDate() WHERE [ID] = {@id}"; 
            //todo:将{@title}替换为title的值;将{@content}替换为content的值;将{@id}替换为id的值。通过正则表达式"\\{\\@(\\w)+\\}"可以匹配出其中的{@title};{@content};{@id}"; 
            return sql; 
        } todo就是我要实现的功能