一起重新开始学大数据-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

相关文章

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

springboot上传zip包并解压至服务器nginx目录方式

《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录springboot上传zip包并解压至服务器nginx目录1.首先需要引入zip相关jar包2.然

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七

Java数组初始化的五种方式

《Java数组初始化的五种方式》数组是Java中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代