首页 \ 问答 \ 如何在java中获得类注解?(How to get class annotation in java?)

如何在java中获得类注解?(How to get class annotation in java?)

我已经创建了我自己的注释类型,如下所示:

public @interface NewAnnotationType {}

并附加到一个类:

@NewAnnotationType
public class NewClass {
    public void DoSomething() {}
}

我试图通过像这样的反射来获得类注解:

Class newClass = NewClass.class;

for (Annotation annotation : newClass.getDeclaredAnnotations()) {
    System.out.println(annotation.toString());
}

但它不打印任何东西。 我究竟做错了什么?


I have created my own annotation type like this:

public @interface NewAnnotationType {}

and attached it to a class:

@NewAnnotationType
public class NewClass {
    public void DoSomething() {}
}

and I tried to get the class annotation via reflection like this :

Class newClass = NewClass.class;

for (Annotation annotation : newClass.getDeclaredAnnotations()) {
    System.out.println(annotation.toString());
}

but it's not printing anything. What am I doing wrong?


原文:https://stackoverflow.com/questions/17908771
更新时间:2023-10-16 22:10

最满意答案

import MySQLdb
conn=MySQLdb.connect(host='localhost',
                                     user='db_user',
                                     passwd='db_passwd',
                                     db='db_name',
                                     port=3306)
cur=conn.cursor()
cur.execute('INSERT INTO `uid`(`id`,`key`) VALUES (%s,%s)'
                        ,(1234,"45678")
                        )
conn.commit()
conn.close()

其他回答

我之前也遇到过这样的问题,你看下数据表的字段是否存在默认值为"无"的字段,如果有的话请把默认值修改成'',也就是空值.

相关问答

更多
  • 1234>>> li = [1,2,4,5]>>> li.insert(2,3)>>> li[1, 2, 3, 4, 5]list.insert(index,var),第一个参数是列表索引,指向要插入的位置;第二个参数是要插入的元素
  • import MySQLdb conn=MySQLdb.connect(host='localhost', user='db_user', passwd='db_passwd', db='db_name', port=3306) cur=conn.cursor() cur.execute('INSERT INTO `uid`(`id`,`key`) VALUES (%s,%s)' ,(1234,"45678") ) conn.commit() conn.close()
  • 我想我想通了。 我将在此处添加信息以防其他人遇到此问题: 我需要在脚本中添加conn.commit() Think I figured it out. I will add the info here in case someone else comes across this question: I need to add conn.commit() to the script
  • 实际上,我认为您需要使用MongoDB管理服务等工具收集一些统计数据,然后确定哪些是您的瓶颈。 这里是我的建议: 尝试使用multiprocessing并执行并行Python脚本来插入文档。绝对会有更好的性能。 看看这个例子 。 为了最大化写入性能,分片是一个好方法。 检查你的磁盘I / O速度,也许你可以尝试SSD。 从写入操作性能中查看更多细节。 希望这可以帮助。 Actually,I think you need to collect some statistics by using a tool l ...
  • Mysql Server已经消失了 这个问题在http://dev.mysql.com/doc/refman/5.7/en/gone-away.html中有详细描述,通常的原因是服务器因任何原因断开连接,通常的补救措施是重试查询或重新连接并重试。 但是,为什么这会破坏您的代码是因为您编写代码的方式。 见下文 可能是因为我写的代码很差? 既然你问过。 rc = cursor.execute("SELECT LENGTH(text) FROM table WHERE name = '{0}'".format(f ...
  • 了解交易和隔离级别 。 Learn about transactions and isolation levels.
  • 你必须在一个iterable中传递参数 - 通常是一个元组: query = 'SELECT id,sing_name,bir_yr FROM singers_list WHERE bir_yr = %s' curs.execute(query, (year, )) 注意我也换了? %s占位符。 另请注意,MySQL驱动程序会自动处理 Python和MySQL之间的类型转换 ,如果需要可以放置引号并转义参数以保证您免受SQL注入攻击。 You have to pass the parameters ins ...
  • 您的SQL查询不正确。 它应该是 cursor.execute("INSERT INTO micro (brand, productPrice, SKU, productID, mpn, ean) VALUES (%s,%s,%s,%s,%s,%s)",(d['brand'], d['productPrice'], d['SKU'], d['productID'],d['mpn'], d['ean'])) Your SQL query is incorrect. It should be cursor.e ...
  • 根据源代码 , paramstyle是format ,所以你需要改变...... cur.execute("INSERT INTO ... VALUES (?,?,?,?,?,?)", v) ...至... cur.execute("INSERT INTO ... VALUES (%s,%s,%s,%s,%s,%s)", v) 如果您正在使用InnoDB等事务存储引擎,则必须在执行INSERT查询后通过调用conn.commit()显式提交事务。 According to the source code ...
  • 看起来你的mock缺少对游标的引用 mock_pymysql.connect.return_value.cursor.return_value.__enter__.return_value = mock_cursor 我总是发现模拟调用语法很尴尬,但MagicMock以迂回方式显示出错了。 它说它没有为pymysql.connect.return_value.cursor注册的返回值 It lo ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。