我需要使用 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());
添加回答
举报
0/150
提交
取消