JDBC

110 篇文章 0 订阅
88 篇文章 1 订阅

JDBC

Java  databses  connection

数据库管理系统: 数据库+应用程序+操作系统+硬件系统

应用程序连接数据库 -----jdbc

1:jdbc的执行原理:(执行步骤,工作流程)

1.1:加载驱动

1.2:建立连接

1.3:准备SQL语句

1.4:执行SQL语句

1.5:处理结果

1.6:断开连接,释放资源

2:程序

2.1导入jar包:

Jar包里面就是一些别人写好的类和接口;

2.1.1在项目下新建一个lib目录

2.1.2把jar包放进去

2.1.3右击bulidpath

2.2写程序

2.2.1异常

java.lang.ClassNotFoundException: com.mysql.jdbc.Driverw

类找不到  说明类名写错了

Unknown database 'bigdata34' 数据库名字错

Access denied for user 'xudemig'@'localhost' (using password: YES)

账号没有定义  或者 密码错误

2.3说得多还不如上码

/**
 * 
 */
package com.zhiyou.U;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;

/**
 * @author Administrator
 *
 */
public class ConnectJdbc {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		insertOneData();
		queryData();
	}
	
	public static void insertOneData() {

//		1获取驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("加载驱动成功!");

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("加载失败!");
			e.printStackTrace();
		}

//		建立(获取)连接
//		url:协议 子协议://ip:端口号/数据库名
//		子协议指的是连接不同的数据库,这里是MySQL,可能是oracle
		String url = "jdbc:mysql://localhost:3306/school";
		String user = "root";
		String password = "123456";
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, user, password);
			System.out.println("连接成功!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("连接失败!");
			e.printStackTrace();
		}

//		执行SQL的statement对象
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		执行SQL
		String sql = "insert into employer (e_name,e_age) values('张三',21)";
		try {
			boolean isOk = statement.execute(sql);
			System.out.println(isOk ? "插入成功" : "插入失败");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

//		释放资源
		try {
			if (statement != null) {
				statement.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (connection != null) {
				connection.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void queryData() {
//		获取驱动
//		registerDriver注册一次
//		new Driver又注册一次
//		查看源代码注册两次 
//		DriverManager.registerDriver(new Driver);

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		建立连接
		Connection connection = null;
		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "123456");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		执行SQL的statement对象
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		执行查询SQL语句
		try {
			ResultSet rSet = statement.executeQuery("select * from employer e left join dept d on e.d_id=d.d_id");
			while (rSet.next()) {
				int e_id = rSet.getInt("e_id");
				String e_name = rSet.getString("e_name");
				int e_age = rSet.getInt("e_age");
				System.out.println(e_id + e_name + e_age);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if (statement != null) {
				statement.close();
			}
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值