如何在java中动态创建一个类 [英] How to create a class dynamically in java

查看:147
本文介绍了如何在java中动态创建一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这甚至有可能。总之,这里是我的问题: 我想创建一个类有一个数据库表模式,比如假设我有一个像

I don't know if this is even possible. Anyway, here is my problem: I want to create a Class having a database table schema, for example suppose that I have a table like

id - unsigned int 
username - varchar(128)
password - varchar(128)

和假设我可以从我的数据库查询数据。我想要做的是动态创建(和,当然,实例化)应该是这样的,一个Java类:

and let's assume I can query this data from my db. What I want to do is to dynamically create (and, of course, instantiate) a Java class that should look like this:

public class User{
    private unsigned int id;
    private String username;
    private String password;
}

(实际上是的ActiveRecord 我的表)

你能不能帮我开始呢? Tnks

Can you help me starting this? Tnks

推荐答案

请问您的执行的具有动态创建和实例化,没有您的其他code知道类?

What would you do with a dynamically created and instantiated class that none of your other code knows about?

对于像Java一stically类型的语言,这是毫无道理有这样的类。而另一方面,大多数或映射器如Hibernate附带的工具,让您从静态数据库架构生成类。

For a stically typed language like Java, it makes little sense to have such classes. On the other hand, most OR Mappers like Hibernate come with tools that allow you to statically generate classes from a database schema.

这篇关于如何在java中动态创建一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆