import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Select {
public static void main(String[] args) throws Exception {
String driverName = "com.mysql.cj.jdbc.Driver"; // 驱动程序名
String userName = "root"; // 数据库用户名
String userPwd = ""; // 密码
String dbName = "students"; // 数据库名
String url = "jdbc:mysql://localhost:3306/" + dbName + "?useSSL=false&serverTimezone=UTC"; // 形成带数据库读写编码的数据库连接字
Class.forName(driverName); // 加载并注册驱动程序
Connection conn = DriverManager.getConnection(url, userName, userPwd);// 创建连接对象
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement stmt = conn.createStatement();// 在桥conn上直接创建一辆汽车
String sql = "select * from stu where cj>=0 and cj<=100";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();// 执行,得到查询结果集合
System.out.println("记录号 学号 姓名 成绩");
while (rs.next()) {
int a = rs.getRow();
int b = rs.getInt("xh");
String c = rs.getString("name");
int d = rs.getInt("cj");
System.out.println(a + " | " + b + " | " + c + " | " + d);
}
pstmt.close();
stmt.close();
conn.close();
}
}
Jaba_Web--JDBC 查询记录操作模板
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
javaee jdbc查询操作
【代码】javaee jdbc查询操作。
java-ee servlet java sql bc -
Jaba_Web--JDBC 删除记录操作模板
import java.sql.Connec...
sql java 数据库 驱动程序 mysql -
Java_Web--JDBC 增加记录操作模板
如果不能成功链接数据库,我的博客JAVA中有...
sql 数据库 java 驱动程序 mysql