JDBC----------->数据库深入

JDBC概念:


JDBC(Java Data Base Connectivity数据库连接)是一种用于执行SQL语句的java API,可以为多种关系数据库提供统一访问,它是由一组用java语言编写的类和接口组成。


JDBC常用的接口函数:


Connection接口

Statement接口

PreparedStatement接口

ResultSet接口

CallableStatement接口

 DriverManager接口


JDBC配置驱动:


把mysql驱动包直接粘贴到工程目录下-->选中jar包右击-->Build Path-->Add to BuildPath


JDBC驱动加载以及获取连接:


1.加载驱动

Class.forName("com.mysql.jdbc.Driver");


2.获取连接

DriverManager.getConnection(jdbc:mysql://地址:端口/库名","root","admin");


JDBC的增删查改:


1、获取连接

2、获取Statement对象

      Connection.createStatement()

3、整理查询的sql语句字符串

    String sql="sql语句"

4、发送并执行sql语句

   ResultSet rs = Statement.executeQuery(sql语句);

5、处理结果集

6、关闭连接


JDBC代码实战:

1.获取连接:

public static Connection getConnection(){
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
			con = DriverManager.getConnection(url, user, psw);//获取连接
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

2.增加一条数据:

public static void insertValue(Teacher tea){
		Connection con = getConnection();
		try {
			Statement state = con.createStatement();//获取程序接口statement对象
			String sql = "insert into teacher(id,name,gender,age,job,creatDate)"
					+ " values(?,?,?,?,?,?)";//创建sql语句对象(预制对象)
			PreparedStatement pstate = con.prepareStatement(sql);
			pstate.setInt(1, tea.getId());
			pstate.setString(2, tea.getName());
			pstate.setString(3, tea.getGender());
			pstate.setInt(4, tea.getAge());
			pstate.setString(5, tea.getJob());
			pstate.setString(6, tea.getCreatDate());
			pstate.executeUpdate();
			state.close();//关闭程序接口
			con.close();//关闭连接
			System.out.println("执行完成...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

结果图示:

JDBC----------->数据库深入_第1张图片


这里的增加数据代码涉及到了预置对象,就是values()里面加上问号'?',可以动态添加数据对象的信息。

3.删除一条数据:

public static void delectValue(){
		Connection con = getConnection();
		try {
			Statement state = con.createStatement();//获取程序接口statement对象
			String sql = "delete from teacher where id=2";//创建sql语句对象
			int executeUpdate = state.executeUpdate(sql);//执行语句并返回受影响的行数
			System.out.println(executeUpdate);
			state.close();//关闭程序接口
			con.close();//关闭连接
			System.out.println("执行完成...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

图示结果:

JDBC----------->数据库深入_第2张图片

4.修改一条数据:

public static void updateValue(){
		Connection con = getConnection();
		try {
			Statement state = con.createStatement();//获取程序接口statement对象
			String sql = "update teacher set name='newName' where name='wangze'";//创建sql语句对象
			int executeUpdate = state.executeUpdate(sql);//执行语句并返回受影响的行数
			System.out.println(executeUpdate);
			state.close();//关闭程序接口
			con.close();//关闭连接
			System.out.println("执行完成...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

图示结果:

JDBC----------->数据库深入_第3张图片


5.查询数据:

public static void queryValue(){
		Connection con = getConnection();
		try {
			Statement state = con.createStatement();//获取程序接口statement对象
			String sql = "select * from teacher";//创建sql语句对象
			ResultSet rs = state.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String gender = rs.getString("gender");
				int age = rs.getInt("age");
				String job = rs.getString("job");
				Date date = rs.getDate("creatDate");
				System.out.println("id="+id+" name="+name+" gender="
						+gender+" age="+age+" job="+job+" date="+date);
			}
			state.close();//关闭程序接口
			con.close();//关闭连接
			System.out.println("执行完成...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

图示结果:

JDBC----------->数据库深入_第4张图片




更多参考资料:

JDBC的PPT文档

JDBC的jar包

你可能感兴趣的:(数据库,mysql,jdbc,api)