亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用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 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號