如何在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()
其他回答
我之前也遇到过这样的问题,你看下数据表的字段是否存在默认值为"无"的字段,如果有的话请把默认值修改成'',也就是空值.
相关问答
更多-
python3使用pymysql模块往mysql插入数据时报错[2022-01-11]
1234>>> li = [1,2,4,5]>>> li.insert(2,3)>>> li[1, 2, 3, 4, 5]list.insert(index,var),第一个参数是列表索引,指向要插入的位置;第二个参数是要插入的元素 -
python pymysql 怎么插入数据[2023-02-06]
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 ...
-
经常读取后,PyMySQL抛出'BrokenPipeError'(PyMySQL throws 'BrokenPipeError' after making frequent reads)[2022-05-08]
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 ... -
PyMySQL选择不工作(PyMySQL Select not working)[2022-12-03]
了解交易和隔离级别 。 Learn about transactions and isolation levels. -
查询中的PyMySQL变量(PyMySQL variables in queries)[2023-05-11]
你必须在一个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 ... -
使用pymysql插入表(use pymysql to insert table)[2022-12-31]
您的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 ... -
Python运算符pymysql(Python operators pymysql)[2022-04-26]
根据源代码 , 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 ... -
你如何模拟PyMySQL的返回值以便在Python中进行测试?(How do you mock a return value from PyMySQL for testing in Python?)[2022-04-13]
看起来你的mock缺少对游标的引用 mock_pymysql.connect.return_value.cursor.return_value.__enter__.return_value = mock_cursor 我总是发现模拟调用语法很尴尬,但MagicMock以迂回方式显示出错了。 它说它没有为pymysql.connect.return_value.cursor注册的返回值It lo ...