java jdbc in查询,Java中的SQL查询:(JDBC)如何使用SELECT语句?

This is my query to connect to my database.

SELECT naam, kleur, sector, aantalZilverstukken, Spel_naam

FROM speler

WHERE Spel_Naam = ?

I work in the console of netbeans. When I want to show the records of table Speler with the Spel_Naam.

In the console I want to type a primary key of the table Spel and then it shows me the records of the table Speler in the console. How can I do this.

Like WHERE Spel_Naam = ?

The question mark need to be the name that I typed in

Is the select statement correct? I want to type the Spel_Naam in the console and then It must connect to the database and give me the records of table Speler. How can I do this?

public class SpelerMapper

{

private final static String LEES_SPELERS_SQL = "SELECT naam, kleur, sector, aantalZilverstukken, Spel_naam FROM speler WHERE Spel_Naam = ?";

public List geefSpelers()

{

List spelerLijst = new ArrayList();

Statement statement;

Connection connection = PersistentieController.getInstance().getConnection();

try

{

statement = connection.createStatement();

// query database

ResultSet resultSet = statement.executeQuery(LEES_SPELERS_SQL);

while (resultSet.next())

{

String naam = resultSet.getString("naam");

String kleur = resultSet.getString("kleur");

int sector = resultSet.getInt("sector");

int aantalZilverstukken = resultSet.getInt("aantalZilverstukken");

Speler speler = new Speler(naam ,kleur, sector , aantalZilverstukken);

spelerLijst.add(speler);

}

statement.close();

return spelerLijst;

} catch (SQLException e)

{

e.printStackTrace();

}

return null;

}

解决方案

Use PreparedStatements:

String LEES_SPELERS_SQL = "SELECT ... WHERE Spel_Naam = ?";

PreparedStatement prepStmt = con.prepareStatement(LEES_SPELERS_SQL);

prepStmt.setString(1, naam);

ResultSet rs = prepStmt.executeQuery();

Additional note: while being another option, concatenation of the SQL query is an unsafe way of doing the same task. Refer to this article for more info.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值