一起重新开始学大数据-MySQL篇-Day37-sql(5)-shell操作MySQL,java操作MySQL、附带JDBC的NullPointerException

本文主要是介绍一起重新开始学大数据-MySQL篇-Day37-sql(5)-shell操作MySQL,java操作MySQL、附带JDBC的NullPointerException,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一起重新开始学大数据-MySQL篇-Day37-sql(5)

在这里插入图片描述

shell操作mysql

在这里插入图片描述

shell操作mysql

#!/bin/sh
MYSQL="mysql -hmaster -uroot -p123456"
sql="select * from bigdata.students where sex='0'"
result="$($MYSQL -e "$sql")"
echo  -e "$result"

在这里插入图片描述

java操作mysql

在这里插入图片描述

导入第三方工具包

file->project structure->Modules->Dependencies->+ ->JARs or dir…->选择包->apply
在这里插入图片描述

创建一个demo1试试看

1.加载第三方工具

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

2.获取连接

  String url="jdbc:mysql://master:3306/shujia";String username="root";String password="123456";Connection connection = DriverManager.getConnection(url, username, password);

3.获取执行器 createStatement(会出现sql注入不使用)和prepareStatement

// Statement statement = conection.createStatement();
// ResultSet rs = statement.executeQuery(sql);String sql="select * from usr where id=? and name=?";PreparedStatement ps = connection.prepareStatement(sql);//给sql的格式(模板)ps.setString(1,"1012");ps.setString(2,"test");

4.获取结果(sql语句为增删改查操作,不需要解析结果,使用executeUpdate())

   ResultSet rs = ps.executeQuery();while (rs.next()){String name = rs.getString("name");System.out.println(name);}

5.关闭连接(从下向上关闭)

   rs.close();ps.close();conn.close();

sql注入:参数中有mysql命令,而mysql把这些关键字当做命令去执行

prepareStatement:避免了sql注入,首先发送sql的格式,然后在传递参数(参数中有关键字也作为参数执行)
prepareStatement传参:通过set数据类型(int prepareIndex,数据类型 x)

注意:
index从1开始

创建连接和关闭需要写很多次,可以把连接和关闭写入到工具类中,再次使用时,直接调用工具类,避免多次书写创建连接和关闭

public class JDBCUtil {private static String driver="com.mysql.jdbc.Driver";private static String url;private static String username="root";private static String password="123456";static {try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}//获取连接的方法public static Connection getConnection() throws Exception {Connection conn = DriverManager.getConnection(url, username, password);return conn;}public static void closeAll(PreparedStatement ps,Connection conn)throws Exception{ps.close();conn.close();}public static void closeAll(ResultSet rs,PreparedStatement ps, Connection conn)throws Exception{rs.close();ps.close();conn.close();}
}

创建资源目录

背景Ⅰ:由于如果每查一次数据库,都要对数据库连接登录等操作,这样就会浪费精力,如果将这些操作进行封装起来,就能更好地节约时间

背景Ⅱ:如果使用同一种对数据库的访问代码,但由于修改了数据库服务器的ip或者访问的用户密码,代码就不能使用了

1.创建普通目录(建议名称为 resource)
在这里插入图片描述

2.通过project st…设置为资源目录
在这里插入图片描述

创建配置文件(创建在资源目录中)

1.格式必须后缀为properties

在这里插入图片描述

将登入时所需填写的数据写入到这个文件中(如果更换ip,或者密码,只需在这个文件里修改,代码依旧可用)

driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://master:3306/bigdata"
user="root"
password="123456"

完整的jdbc工具类代码

package MySQL;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;public class JDBCUtil {private  static String Driver;private  static  String url;private  static  String username;private static  String password;static {//静态代码块try{ClassLoader classLoader = JDBCUtil.class.getClassLoader();InputStream is = classLoader.getResourceAsStream("test.properties");Properties properties = new Properties();try {properties.load(is);}catch (Exception e){e.printStackTrace();}Driver=properties.getProperty("driver");url=properties.getProperty("url");username=properties.getProperty("username");password=properties.getProperty("password");}catch (Exception e){e.printStackTrace();}}public static Connection getConection(){//获取连接方法Connection connection=null;try{Class.forName(Driver);connection = DriverManager.getConnection(url, username, password);}catch (Exception e){e.printStackTrace();}return connection;}//关闭public static void closeALL(ResultSet rs,PreparedStatement ps, Connection conn){if (rs!=null){try {rs.close();}catch (Exception e){e.printStackTrace();}}if (conn!=null){try {conn.close();}catch (Exception e){e.printStackTrace();}}if (ps!=null){try {ps.close();}catch (Exception e){e.printStackTrace();}}}}

注意:(空指针异常Exception in thread “main” java.lang.NullPointerException)

在这里插入图片描述

出现空指针异常:
问题所在位置:(图示为正确的✔)

// 加载配置文件中的数据InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("test.properties");

探究
(使用getResourceAsStream方法,就会在当前Resource类型文件夹下寻找,所以给文件名即可)
|
|

案例①:查询表的创表结构

//案例:查询表的创表结构
package MySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo02 {public static void main(String[] args) throws Exception {Connection conection = JDBCUtil.getConection();String sql="show create table students";PreparedStatement preparedStatement =conection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){String name = resultSet.getString("Create Table");System.out.println(name);}JDBCUtil.closeALL(resultSet,preparedStatement,conection);}
}

案例②查stundents表中,id为1001,sex为男的人的名字

package MySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;public class Demo02 {public static void main(String[] args) throws Exception {Connection conection = JDBCUtil.getConection();String sql="select * from students where id=? and sex=?";PreparedStatement preparedStatement =conection.prepareStatement(sql);preparedStatement.setString(1,"1001");preparedStatement.setString(2,"1");ResultSet resultSet = preparedStatement.executeQuery();while (resultSet.next()){String name = resultSet.getString("name");System.out.println(name);}JDBCUtil.closeALL(resultSet,preparedStatement,conection);}
}

在这里插入图片描述

案例③查询性别为男的学生信息(1为男,0为女)

package MySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo02 {public static void main(String[] args) throws Exception {Connection conection = JDBCUtil.getConection();String sql="select * from students where sex=?";PreparedStatement preparedStatement =conection.prepareStatement(sql);preparedStatement.setString(1,"1");ResultSet resultSet = preparedStatement.executeQuery();System.out.println(" id"+"  "+" name"+"  "+" age"+"  "+" sex");//纯属为了观看while (resultSet.next()){String id = resultSet.getString("id");String age = resultSet.getString("age");String name = resultSet.getString("name");String sex = resultSet.getString("sex");System.out.println(id+"  "+name+"  "+age+"    "+sex);}JDBCUtil.closeALL(resultSet,preparedStatement,conection);}
}

在这里插入图片描述
|
|
|
|
|
|

上一章-MySQL篇-Day36-case值替换,备份表,稍微了解一下视图,事务

下一章-Maven先导篇-Day38-安装配置maven,Git

|
|
|
|
|

听说长按大拇指👍会发生神奇的事情呢!好像是下面的画面,听说点过的人🧑一个月内就找到了对象的💑💑💑,并且还中了大奖💴$$$,考试直接拿满分💯,颜值突然就提升了😎,虽然对你好像也不需要,是吧,吴彦祖🤵!

在这里插入图片描述 在这里插入图片描述
在这里插入图片描述

这篇关于一起重新开始学大数据-MySQL篇-Day37-sql(5)-shell操作MySQL,java操作MySQL、附带JDBC的NullPointerException的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/871496

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477