Java的第十七章:数据库操作

 数据库基础 

 SQL语言

1、select 语句

select 语句用于从数据中检索数据。语法如下:

SELECT 搜选字段列表 FROM 数据表名

WHERE 条件表达式 GROUP BY 字段名 HAVING 条件表达式(指定分组的条件)

ORDER BY 字段名[ASC|DESC]

2、insert 语句

insert 语句用于向表中插入新数据。语法如下:

insert into 表名[(字段1,字段2...)]values(属性值1,属性值2);

 3、update 语句

update 语句用于更新数据表中的某些记录。语法如下:

UPDATE 数据表名 SET 字段名=新的字段值 WHERE 条件表达式;

 4、delete 语句

delete 语句用于删除数据。语法如下:

delete from 数据表名 where 条件表达式;

 JDBC中常用的类和接口

1、 DriverManager 类

       DriverManager 类师JDBC的管理层,用于管理数据库中的驱动程序。在操作指定数据库之前,需要使用Java中Class 类的静态方法forName(String className)加载指定数据库的驱动程序。

2、Connection 接口

      Connection 接口代表与特定的数据库的连接,在连接上下文中执行SOL语句并返回结果。

3、Statement 接口

      Statement 接口用于在已经建立连接的基础上向数据库发送SQL语句。

4、PreparedStatement 接口

       PreparedStatement 接口用来动态地执行SQL语句。通过PreparedStatement 实例执行的动态的SQL语句,将被预编译并能保存到PreparedStatement 实例中,从而可以反复地执行该SQL语句。

5、ResultSet 接口

       ResultSet 接口类似与一个临时表,用来展示存放数据库查询操作所获得的结果。ResultSet 实例具有指定当前数据行的指针,指针开始的位置在第一个记录的前面,通常next()方法可将指针向下移。

数据库操作

      要对数据库表中的数据库进行操作,首先应该建立与数据库的连接。通过JDBC API 中提供的各种各类,可对数据表中的数据进行查找、添加、修改、删除等操作。代码如下:

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

import com.mysql.cj.jdbc.Driver;


public class JDBC {
	Connection connection;

	public static void main(String[] args)throws  SQLException {
	JDBC jdbc=new JDBC();
	jdbc.getConnection();
	System.out.println("查询所有课程,结果为:");
	jdbc.selectAll();
	System.out.println("新增4号课程,名为Web");
	jdbc.add(4,"Web");
	System.out.println("查询所有课程,结果为:");
	jdbc.selectAll();
	System.out.println("修改三课程,名为office");
	jdbc.update(3,"office");
	System.out.println("查询所有课程,结果为:");	
	jdbc.selectAll();	
	System.out.println("删除4号课程");
	jdbc.delete(4);
	System.out.println("查询所有课程,结果为:");
	jdbc.selectAll();
	jdbc.close();
	}
	//删除课程
	public void delete(int id) throws SQLException {
		//第三步:获取statement对象
		PreparedStatement preparedStatement=
	connection.prepareStatement("delete from couse where id= ?;");
		preparedStatement.setInt(1,id);
		//执行SQL语句返回结果集
		preparedStatement.executeUpdate();
		
	}
	//修改课程
	public void update(int id,String name) throws SQLException {
		//第三步:获取statement对象
		PreparedStatement preparedStatement=
connection.prepareStatement("update couse set name= ? where id= ?;");
		preparedStatement.setString(1, name);
		preparedStatement.setInt(2, id);
		//第四步: 获取statement对象
		preparedStatement.executeUpdate();
	}
	//添加课程
	public void add(int id, String name)throws SQLException {
		//第三步:获取statement对象
		PreparedStatement preparedStatement=connection.prepareStatement("insert into couse value(?,?);");
		
		preparedStatement.setInt(1, id);
		preparedStatement.setString(2, name);
		//第四步:执行SQL语句返回结果集
		preparedStatement.executeUpdate();
	}

	public void getConnection()throws SQLException{
		//第一步注册驱动	
		DriverManager.registerDriver(new Driver());	
	//第二步:获取连接		
		connection=DriverManager.getConnection(
		"jdbc:mysql://localhost:3306/school_java","root","1234");
	
		}
	
	//查询所有课程
	public void selectAll()throws SQLException {
	//第三步:获取statement对象
	PreparedStatement preparedStatement=
	connection.prepareStatement("select * from couse;");
//第四步: 执行SQL语句返回结果集
	ResultSet resultSet=preparedStatement.executeQuery();
//第五步:遍历结果集
		while(resultSet.next()) {
			System.out.print(resultSet.getInt("id")+" ");
			System.out.println(resultSet.getString("name"));
		}
	//第六步:关闭连接释放资源
		resultSet.close();
		preparedStatement.close();
	
	}
	public void close()throws SQLException {
		connection.close();
	}
}

运行结果:

猜你喜欢

转载自blog.csdn.net/nmy15570188135/article/details/133745373
今日推荐