用java&;从数据库中检索数据;myphpadmin

用java&;从数据库中检索数据;myphpadmin,java,database,phpmyadmin,Java,Database,Phpmyadmin,将要求用户输入一个E_Id(PK)以检索该员工的“工资”数据。例如,中的用户键(例如105)将返回2600的数据。当用户每次输入不同的PK以获得不同的数据时,我应该写什么查询?我已成功连接到神话管理员 SELECT salary FROM Employee WHERE E_ID = ?????? (supposed to be the input of user) 提前感谢最简单的方法是使用JDBC语句。基本查询如下所示: Connection conexion = DriverManager

将要求用户输入一个
E_Id
(PK)以检索该员工的“工资”数据。例如,中的用户键(例如105)将返回2600的数据。当用户每次输入不同的PK以获得不同的数据时,我应该写什么查询?我已成功连接到神话管理员

SELECT salary FROM Employee WHERE E_ID = ?????? (supposed to be the input of user)

提前感谢

最简单的方法是使用
JDBC语句
。基本查询如下所示:

Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/test", "root","root");
Statement statement= conexion.createStatement();
String id="105"; //Here you would need to get the input of the user
String query= "select Salary from Employee where E_ID='"+id+"'";
ResultSet rs=statement.executeQuery(query);
另一种更安全的方法是使用
Prepared语句
,以避免SQL注入

String query= "select Salary from Employee where E_ID= ? ";
Connection conexion= DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root");
PreparedStatement ps= conexion.prepareStatement(query);
sentencia.setString(1, "105");
ResultSet rs = ps.executeQuery();

这个查询看起来很合理(只需使用
Salary
而不是
Salary
),那么问题是什么呢?你是否创建了一个新用户来询问一个与此略有不同的问题,或者这是一个家庭作业,并且有一群新用户询问这个问题?因为如果这是第一个案例,你应该等到有人重新打开它,因为你修改了它。@FedericoklezCulloca正是我的想法:)