java jdbc使用步骤

本文介绍如何使用Java JDBC进行数据库操作,包括注册驱动、建立连接、执行SQL语句和遍历结果集等关键步骤,并提供了一段完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1注册驱动(不必要)
DriverManager.registerDriver(new Driver());
2取得连接
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost/stcxxxx?useUnicode=true&characterEncoding=utf-8&useSSL=false”, “root”, “123456”);
3取得statement对象
Statement st = con.createStatement();
4 sql语句
String sql = “select * from student”;
5执行sql语句
ResultSet rs = st.executeQuery(sql);
6遍历结果
while(rs.next())
{
System.out.println(rs.getInt(“Sno”)+" "+rs.getString(“Sname”));

		}

7关闭连接一步一步向上关闭
rs,st,con
标准代码

	Connection con=null;
		Statement st=null;
		ResultSet rs=null;
		try {
			//DriverManager.registerDriver(new Driver());
			con = DriverManager.getConnection("jdbc:mysql://localhost/stc_yangzeyu?useUnicode=true&characterEncoding=utf-8&useSSL=false", "root", "123456");
			st = con.createStatement();
			String sql = "select * from student";
			rs = st.executeQuery(sql);
			while(rs.next())
			{
				System.out.println(rs.getInt("Sno")+"  "+rs.getString("Sname"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				st.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

建立连接工具包

public class JdbcUtil {
//	public static String driverClass=null;
//	public static String url=null;
//	public static String user=null;
//	public static String password=null;
//	static {
//		InputStream is =null;
//		try {
//			Properties properties= new Properties();
//			is= JdbcConnect.class.getClassLoader().getResourceAsStream("jdbc.properties");
//			properties.load(is);
//			driverClass= properties.getProperty(driverClass);
//			url=properties.getProperty(url);
//			user=properties.getProperty(user);
//			password=properties.getProperty(password);
//			
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	public  Connection getCon()
	{
		Connection conn =null;
		InputStream in= null;
		try {
			in = this.getClass().getResourceAsStream("jdbc.properties");
			Properties prop = new Properties();
			prop.load(in);
			String driver =prop.getProperty("driverClass");
			String url = prop.getProperty("url");
			String username = prop.getProperty("user");
			String password = prop.getProperty("password");
//			Class.forName(driver);
			conn =DriverManager.getConnection(url, username, password);
			
			
		} catch (IOException  | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return conn;
	}
	
}
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/stc_yangzeyu?useUnicode=true&characterEncoding=utf-8&useSSL=false
user=root
password=123456

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值