JDBC简单实现

本文提供了一个简单的JDBC使用示例,涵盖了连接数据库、创建表、插入数据、更新记录、查询数据、删除记录及表等基本操作。适用于初学者快速上手。

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

本着实用性的原创,去除JDBC一些深入的东西。只是为了一个很好复习和或者入门,一个总结。

public class TestJDBC {
	
	private static Connection connection = null;
	
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		
		connection = getConnection();
		Statement statement = connection.createStatement();
		//建表
		//statement.execute("create table mytable(id integer,name varchar(20),password varchar(20))");
		//插入数据
		//statement.execute("insert into mytable values(1,'name','name')");
		//更新
		//statement.execute("update mytable set name='lll' where id=1");
		//查询表内容
		/*
		ResultSet set = statement.executeQuery("select * from mytable");
		ResultSetMetaData meta = set.getMetaData();
		
		while(set.next()){
			for(int i=1;i<=meta.getColumnCount();i++){
				Object o = set.getString(i);
				System.out.print(o+" ");
			}
			System.out.println();
		}*/
		//删除
		//statement.execute("delete from mytable where id=1");
		//删除表
		//statement.execute("drop table mytable");
		
		statement.close();
		connection.close();
		
	}
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/Test","root","");
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值