mysql的retrieve

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();
}
}
}

mysql的retrieve

上一篇:高并发场景下,先更新缓存还是先更新数据库


下一篇:Java数据库编程JDBC总结