您的位置:首页 > 游戏 > 游戏 > seo优化网站快速排名_国内域名备案_seo推广工具_最新新闻热点事件2022

seo优化网站快速排名_国内域名备案_seo推广工具_最新新闻热点事件2022

2024/12/23 6:12:21 来源:https://blog.csdn.net/a624135_/article/details/143403250  浏览:    关键词:seo优化网站快速排名_国内域名备案_seo推广工具_最新新闻热点事件2022
seo优化网站快速排名_国内域名备案_seo推广工具_最新新闻热点事件2022

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;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com