JDBC查询所有记录打印到控制台上

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.concurrent.locks.StampedLock;
//查询出所有的记录 打印到控制台上
public class JDBCDemo2 {

	
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
//		1、加载驱动程序并注册驱动:DriverManager
		Class.forName("com.mysql.jdbc.Driver");
//		2、获取与数据库的连接:Connection
		//方式一
//   	Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day14", "root", "gqcmysql");//url 不同数据库不同 具体看数据库说明
		//方式二
//		Properties info=new Properties();
//		info.setProperty("user", "root");//key 要查阅数据库的文档
//		info.setProperty("password", "gqcmysql");
//		Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day14", info);
		//方式三
		Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day14?user=root&password=gqcmysql");
		
//		3、得到代表SQL语句的对象 并发送 SQL给数据库:Statement
		Statement stmt=conn.createStatement();
	//	int i=stmt.executeUpdate("delete from users");
		//System.out.println(i);
		boolean b=stmt.execute("update users set password='123'");
		System.out.println(b);
	/*	//		4、如果有查询结果、得到封装了查询结果的对象:ResultSet
		ResultSet rs=stmt.executeQuery("select id,name,password,email,birthday from users");
//		5、遍历结果
		while(rs.next()) {
			System.out.println("----------");
			System.out.println(rs.getObject("id"));
			System.out.println(rs.getObject("name"));
			System.out.println(rs.getObject("password"));
			System.out.println(rs.getObject("email"));
			System.out.println(rs.getObject("birthday"));			
		}*/
//		6、释放占用的资源 
	//	rs.close();
		stmt.close();
		conn.close();
	}

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Java代码实现连接数据库,往表中插入自己和张三的名字和学号,再查询表中记录,并把表记录打印控制台上的示例: ```java import java.sql.*; public class DatabaseExample { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 加载JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 建立数据库连接 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); // 往表中插入自己和张三的名字和学号 stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO student (name, id) VALUES ('Me', '001'), ('Zhang San', '002')"); // 查询表中记录打印控制台上 rs = stmt.executeQuery("SELECT * FROM student"); while (rs.next()) { String name = rs.getString("name"); String id = rs.getString("id"); System.out.println("Name: " + name + ", ID: " + id); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 这段代码首先加载了JDBC驱动程序,然后建立了一个MySQL数据库连接。接着使用Statement对象执行SQL语句,往表中插入自己和张三的名字和学号。然后再次使用Statement对象执行SQL查询语句,将查询结果保存在ResultSet对象中,并使用while循环遍历ResultSet对象,将每个记录的姓名和学号打印控制台上。最后关闭了ResultSet、Statement和Connection对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值