为了账号安全,请及时绑定邮箱和手机立即绑定

使用java中的Prepared Statement将用户IP地址插入MySql表

使用java中的Prepared Statement将用户IP地址插入MySql表

翻翻过去那场雪 2023-03-23 16:19:06
我需要使用 PreparedStatement 将用户 IP 地址插入到 MySql 表中。我该怎么做?我尝试在 Servlet 中使用以下代码。InetAddress ipaddress;try {    ipaddress = InetAddress.getLocalHost();} catch (UnknownHostException e) {    e.printStackTrace();}我想知道有没有比上面更好的方法来获取 IP 地址??或者我可以继续上面的代码?String insertquery = "insert into tablename (IPAdd) values (?)";preparedStatement = conn.prepareStatement(insertquery);preparedStatement.setString(1, ipaddress);// It's not a string so How can I set values here ?// Getting error here
查看完整描述

1 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

如果您想获取 IP,则不要使用:

preparedStatement.setString(1, ipaddress);

使用 :

// this will return the IP Host Adresse for example 192.168.1.2
preparedStatement.setString(1, ipaddress.getHostAddress());


查看完整回答
反对 回复 2023-03-23
  • 1 回答
  • 0 关注
  • 115 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号