mysql的retrieve

时间:2021-01-18 11:27:02   收藏:0   阅读:0
package test;

import java.sql.*;

public class retrieve {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql="SELECT * FROM hero ";//查询整个表的数据, select count(*) from hero 查询总数 select * from hero limit 0,5; 查询前五个数据
try (Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/how2java?characterEncoding=utf-8",
"root","123456");
//创建语句
PreparedStatement ps=c.prepareStatement(sql);){
ResultSet rs=ps.executeQuery();
while (rs.next()){
int id=rs.getInt(1);
String name=rs.getString(2);
float hp=rs.getFloat(3);
int damage=rs.getInt(4);
System.out.println(name);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!