1.简介
2.流程
3.JDBC链接数据库并实现增删改查功能
package com.qcby.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class Jdbc {
static final String URL = "jdbc:mysql://localhost:3306/weiyang?useUnicode=true&characterEncoding=utf-8";
static final String ROOT = "root";
static final String PASSWORD = "2020";
public static void main(String[] args) {//search();//查询数据//insert();//添加数据//update();//修改数据//delete();//删除数据
}
//数据的查询
public static void search() {try {// 1.加载驱动Class.forName("com.mysql.jdbc.Driver");// 2.创建connection对象 链接数据库// url:统一资源定位符 :Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);Statement statement = (Statement) connection.createStatement();// sql语句String sql = "select * from student";ResultSet re = statement.executeQuery(sql);// 处理数据while (re.next()) {String idString = re.getString("id");String name = re.getString("name");String height = re.getString("height");String age = re.getString("age");System.out.println(idString + " " + name + " " + height + " " + age);}if(re !=null) {re.close();}if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}
}// 数据的添加
public static int insert() {int temp = 0;try {Class.forName("com.mysql.jdbc.Driver");Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);Statement statement = (Statement) connection.createStatement();String sql = "insert into student(name,height,age) values ('小风',186,23)";statement.executeUpdate(sql); temp++;if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return temp;
}// 数据的修改
public static int update() {int temp = 0;try {Class.forName("com.mysql.jdbc.Driver");Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);Statement statement = (Statement) connection.createStatement();String sql = "update student set name='小黑' ,height = 186,age = 18 where id = 1";statement.executeUpdate(sql); temp++;if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return temp;
}// 数据的删除public static int delete() {int temp = 0;try {Class.forName("com.mysql.jdbc.Driver");Connection connection = (Connection) DriverManager.getConnection(URL,ROOT,PASSWORD);Statement statement = (Statement) connection.createStatement();String sql = "delete from student where id = 7 ";statement.executeUpdate(sql); temp++;if(statement !=null) {statement.close();}if (connection !=null) {connection.close();}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return temp;}