JAVA与Mysql.

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: JAVA与Mysql.

第一部分数据的增删改查。

@1 查看表的结构。

DESC student studentt为数据库

@2 增一条记录。

@3改记录

@4删除记录

#查表的结构
DESC student
SELECT *FROM student;
INSERT INTO `students`.`student`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1001, '毛毛', '男', 12);
INSERT INTO `students`.`student`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1002, '毛二', '男', 30);
INSERT INTO `students`.`student`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1003, '胡滨', '男', 20);
INSERT INTO `students`.`student`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1004, '集合', '男', 20);
DESC studenta;
INSERT INTO `students`.`studenta`(`stuId`, `stuSex`, `stuHeight`, `stuAge`) VALUES (1008, '674', 56, '79');
INSERT INTO `students`.`studenta`(`stuId`, `stuName`, `stuSex`, `stuHeight`, `stuAge`) VALUES (1009, '欣慰', '456', 34, '78');
INSERT INTO `students`.`studenta`(`stuId`, `stuName`, `stuSex`, `stuHeight`, `stuAge`) VALUES (1010, 'ok', '456', 34, '78');
SELECT  *FROM studenta;
select * FROM student;
INSERT INTO `students`.`studenta`(`stuId`, `stuName`, `stuSex`, `stuHeight`, `stuAge`) VALUES (1011, '三体', '456', 34, '78');
INSERT INTO `students`.`studenta`(`stuId`, `stuName`, `stuSex`, `stuHeight`, `stuAge`) VALUES (10012, '学Java很快哦', '345', 34, '67')
INSERT INTO `students`.`student`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1007, 'hellows', '男', 24)
INSERT INTO `students`.`stu`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1007, '胡期', '男', 35);
INSERT INTO `students`.`stu`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (10071, '分解', '男', 35);
INSERT INTO `students`.`stu`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (10072, '信他', '男', 35);
INSERT INTO `students`.`stu`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (10073, 'comon', '男', 35);
INSERT INTO `students`.`stu`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (10074, '自由', '男', 35);
DESC stu;
SELECT * FROM stu;
INSERT INTO `students`.`student`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1006, 'hellow', '男', 24)
INSERT INTO `students`.`stu`(`stuId`, `stuName`, `stuSex`, `stuAge`) VALUES (1075, '小王', '女', 23);
DESC stu;
DESC student
select * FROM student;
UPDATE `students`.`student` SET `stuName` = 'need' WHERE `stuId` = 1007
DELETE FROM `students`.`student` WHERE `stuId` = 1006

回顾了一下mysql的基本语法。

java代码。

@1导入构架包

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCDamo1 {
  public static void main(String[] args)  throws Exception{
    // 加载驱动类
    Class.forName("com.mysql.cj.jdbc.Driver");
    System.out.println("加载驱动类成功");
    //地址
    String url ="jdbc:mysql://127.0.0.1:3306/students";
    System.out.println("mysql连接成功");
    String username="root";
    String password="123456";
    Connection conn=DriverManager.getConnection(url,username,password);
       System.out.println(conn);
       System.out.println(url);
       System.out.println(username);
       System.out.println(password);
       System.out.println(conn.getHoldability());
       System.out.println(conn.getAutoCommit());
       System.out.println(conn.getCatalog());
       System.out.println(conn.getClientInfo(password));
       System.out.println(conn.isClosed());
       System.out.println(conn.isReadOnly());
       System.out.println(conn.isValid(conn.getHoldability()));
       System.out.println(conn.toString());
       System.out.println(conn.equals(username));
       System.out.println(conn.prepareCall(username));
       System.out.println(conn.prepareCall(url));
       System.out.println(conn.prepareCall(password));
       System.out.println(conn.getWarnings());
       System.out.println(conn.setSavepoint());
       System.out.println(conn.createStatement());
       System.out.println(conn.setSavepoint());
       System.out.println(conn.createBlob());
       System.out.print(conn.createSQLXML());
       System.out.println(conn.hashCode());
  }
}
package Gradation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Tran {
    static Connection con;
     static Statement sql;
     static ResultSet res;
    public Connection getConnection() {
      return con;
    }
    public static void main(String[] args) throws Exception {
      Class.forName("com.mysql.cj.jdbc.Driver");
      System.out.println("加载驱动类成功");
      //地址
      String url ="jdbc:mysql://127.0.0.1:3306/student";
      System.out.println("mysql连接成功");
      String username="root";
      String password="123456";
      Connection conn=DriverManager.getConnection(url,username,password);
         System.out.println(conn);
      Gradation c=new Gradation();
      con=c.getConnection();
    try {
      sql=con.createStatement();
      res=sql.executeQuery("select*from tb_stu where"+"name like '胡%'");
      while(res.next()) {
        String id =res.getString("id");
        String name=res.getString("name");
        String sex=res.getString("sex");
        String birthday=res.getString("birthday");
        System.out.println(id);
        System.out.println(name);
        System.out.println(sex);
        System.out.println(birthday);
      }
    } catch (Exception   e) {
      //e.printStackTrace();
    }
    }
}

 


package jdbc;
import java.sql.*;
//查询学生信息
//查询信息
public class JDBCCha {
  public static void main(String[] args) throws Exception {
  // 建立链接
  Connection conn = DBUtil.getConnection();
  //创建statement对象
   Statement stmt =conn.createStatement();
   //执行mysql语句
   String sql="select *from student";
   ResultSet rs =stmt.executeQuery(sql);
//遍历集合
   while(rs.next()) {
     int stuId=rs.getInt("stuId");
     String stuName=rs.getString("stuName");
     String stuSex=rs.getString("stuSex");
     int stuAge=rs.getInt("stuAge");
     System.out.println(stuId+"---------"+stuName+"-----------"+stuSex+"---------"+stuAge);
   }
   //关闭数据库连接
   DBUtil.getClose(conn);
  }
}

package jdbc;
import java.sql.*;
//查询学生信息
//删除信息
public class JDBCDelect {
  public static void main(String[] args) throws Exception {
  // 建立链接
  Connection conn = DBUtil.getConnection();
  //创建statement对象
   Statement stmt =conn.createStatement();
   //执行mysql语句
   String sql="delect from student";
   ResultSet rs =stmt.executeQuery(sql);
//遍历集合
    /*
     * while(rs.next()) {
     * 
     * int stuId=rs.getInt("stuId"); String stuName=rs.getString("stuName"); String
     * stuSex=rs.getString("stuSex"); int stuAge=rs.getInt("stuAge");
     * System.out.println(stuId+"---------"+stuName+"-----------"+stuSex+"---------"
     * +stuAge);
     * 
     * }
     * 
     * //关闭数据库连接 DBUtil.getClose(conn);
     * 
     * 
     * }
     */
  }
}

package jdbc;
import java.sql.*;
public class JDBCShan {
//修改信息
  public static void main(String[] args) throws Exception {
    // 建立链接
  Connection conn = DBUtil.getConnection();
  //
   Statement stmt =conn.createStatement();
   String sql="update student set stuSex='女'";
   stmt.executeLargeUpdate(sql);
   DBUtil.getClose(conn);
  }
}

package jdbc;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
//增加信息
public class JDBCZeng {
  public static void main(String[] args)  throws Exception{
    Connection conn=DBUtil.getConnection();
    System.out.println(conn);
       //获取 statement对象
       Statement stmt =conn.createStatement();
       //增加
       String sql="insert into student (stuId,stuName,stuSex,stuAge) values(1003,'sanmao','女',20)";
       //4  执行语句
       int count=   stmt.executeUpdate(sql);
       if(count>0) {
         System.out.println("学生插入成功");
       }else {
         System.out.println("in the end");
       }
        //5 int the end
       //conn.close();
       DBUtil.getClose(conn);
    // 加载驱动类
      //Class.forName("com.mysql.cj.jdbc.Driver");
      //System.out.println("加载驱动类成功");
      //建立数据库
      //String url ="jdbc:mysql://127.0.0.1:3306/students";
      //System.out.println("mysql连接成功");
      //String username="root";
      //String password="123456";
      //Connection conn=DriverManager.getConnection(url,username,password);
        //System.out.println(conn);        
  }
}
package jdbc;
public class Student {
  private int stuId;
  private String stuName;
  private String  stuSex;
  private int stuAge;
  public Student() {
    super();
    // TODO Auto-generated constructor stub
  }
  public Student(int stuId, String stuName, String stuSex, int stuAge) {
    super();
    this.stuId = stuId;
    this.stuName = stuName;
    this.stuSex = stuSex;
    this.stuAge = stuAge;
  }
  /**
   * @return the stuId
   */
  public int getStuId() {
    return stuId;
  }
  /**
   * @param stuId the stuId to set
   */
  public void setStuId(int stuId) {
    this.stuId = stuId;
  }
  /**
   * @return the stuName
   */
  public String getStuName() {
    return stuName;
  }
  /**
   * @param stuName the stuName to set
   */
  public void setStuName(String stuName) {
    this.stuName = stuName;
  }
  /**
   * @return the stuAge
   */
  public int getStuAge() {
    return stuAge;
  }
  /**
   * @param stuAge the stuAge to set
   */
  public void setStuAge(int stuAge) {
    this.stuAge = stuAge;
  }
  @Override
  public String toString() {
    return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuSex=" + stuSex + ", stuAge=" + stuAge + "]";
  }
}

package Code;
/**
 * java的转码格式
 * @author MZFAITHDREAM
 *
 */
public class CodeDemo1 {
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    String str="我爱距离100km江西南字符昌学lookforyou江西区的学院";
    //将字符串转成编码
    byte [] by =str.getBytes("utf-8");
    //将数据给用户看
     String str1 =new String(by,"utf-8");
     System.out.println(str1);
    //网页定义utf-8 
     String str2="我爱距离100km温州瓯江";
      //将字符串转成编码
      byte [] by1 =str1.getBytes("gbk");
      //将数据给用户看
       String str3 =new String(by,"gbk");
       System.out.println(str2);
    System.out.println(str.getBytes());
    System.out.println(str);
    System.out.println(str2);
  }
}
package readingrandwriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class CopeFile {
  public static void main(String[] args) throws IOException {
  //创建字节输入
    FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\Javaday1_9.zip");
    //创建字节输出  
    FileOutputStream fos =new  FileOutputStream("D:\\Day");
  //字符输入
    InputStreamReader isr=new    InputStreamReader(fis,"utf-8");
  //字符输出
    OutputStreamWriter osr=new   OutputStreamWriter(fos,"utf-8");
  //创建一个数组去放入取到的数据
    char[] cr=new char[1024];
    int num=0;
  //while
    while((num=isr.read(cr))!=-1) {
      //reading
      osr.write( cr,0,num);
    }
    isr.close();
    osr.close();
  }
}
package readingrandwriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class CopeFile2 {
  public static void main(String[] args) throws IOException {
  //创建字节输入
    //FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\Javaday1_9.zip");
    //创建字节输出  
    //FileOutputStream fos =new  FileOutputStream("D:\\Day");
  //字符输入
    //InputStreamReader isr=new    InputStreamReader(fis,"utf-8");
  //字符输出
    //OutputStreamWriter osr=new   OutputStreamWriter(fos,"utf-8");
  FileReader fr=new FileReader("C:\\Users\\MZFAITHDREAM\\Desktop\\青年大学习第10期.xlsx"); 
  FileWriter fw=new FileWriter("D:\\dayop1");
  //创建一个数组去放入取到的数据
    char[] cr=new char[1024];
    int num=0;
  //while
    while((num=fr.read(cr))!=-1) {
      //reading
      fw.write( cr,0,num);
    }
    fr.close();
    fw.close();
  }
}

package repeat;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy1 {
  public static void main(String[] args)  throws IOException{
    // TODO Auto-generated method stub
    FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.jpg");
    FileOutputStream fos =new  FileOutputStream("D:\\胡滨.jpg" ,true);
    byte[] by =new byte[1024];
    //int read(byte[]) by) 
    int num=0;
    while((num=fis.read(by))!=-1) {
      //reading
      fos.write(by, 0, num);
    }
    //关闭流
    fis.close();
    fos.close();
  }
}

package repeat;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy2 {
  public static void main(String[] args)  throws IOException{
    // TODO Auto-generated method stub
    FileInputStream fis= new   FileInputStream("C:\\Users\\MZFAITHDREAM\\Desktop\\1.png");
    FileOutputStream fos =new  FileOutputStream("D:\\hu.png" ,true);
    byte[] by =new byte[1024];
    //int read(byte[]) by) 
    int num=0;
    while((num=fis.read(by))!=-1) {
      //reading
      fos.write(by, 0, num);
    }
    //关闭流
    fis.close();
    fos.close();
  }
}

Exception in thread "main" java.io.FileNotFoundException: C:\Users\MZFAITHDREAM\Desktop\1.jpg (系统找不到指定的文件。)
  at java.io.FileInputStream.open0(Native Method)
  at java.io.FileInputStream.open(FileInputStream.java:195)
  at java.io.FileInputStream.<init>(FileInputStream.java:138)
  at java.io.FileInputStream.<init>(FileInputStream.java:93)
  at repeat.Copy1.main(Copy1.java:13)

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4天前
|
前端开发 关系型数据库 MySQL
【前端学java】MySQL数据库的本地安装
【8月更文挑战第12天】MySQL数据库的本地安装
20 3
|
11天前
|
SQL druid Java
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(下)
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)
34 3
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(下)
|
11天前
|
SQL Java 关系型数据库
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(上)
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)
47 3
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(上)
|
3天前
|
存储 SQL 关系型数据库
深入MySQL锁机制:原理、死锁解决及Java防范技巧
深入MySQL锁机制:原理、死锁解决及Java防范技巧
|
6天前
|
缓存 NoSQL Redis
一天五道Java面试题----第九天(简述MySQL中索引类型对数据库的性能的影响--------->缓存雪崩、缓存穿透、缓存击穿)
这篇文章是关于Java面试中可能会遇到的五个问题,包括MySQL索引类型及其对数据库性能的影响、Redis的RDB和AOF持久化机制、Redis的过期键删除策略、Redis的单线程模型为何高效,以及缓存雪崩、缓存穿透和缓存击穿的概念及其解决方案。
|
6天前
|
算法 关系型数据库 MySQL
一天五道Java面试题----第七天(mysql索引结构,各自的优劣--------->事务的基本特性和隔离级别)
这篇文章是关于MySQL的面试题总结,包括索引结构的优劣、索引设计原则、MySQL锁的类型、执行计划的解读以及事务的基本特性和隔离级别。
|
5天前
|
算法 关系型数据库 MySQL
一天五道Java面试题----第七天(mysql索引结构,各自的优劣--------->事务的基本特性和隔离级别)
这篇文章是关于MySQL的面试题总结,包括索引结构的优劣、索引设计原则、MySQL锁的类型、执行计划的解读以及事务的基本特性和隔离级别。
|
6天前
|
存储 关系型数据库 MySQL
MySQL——数据库备份上传到阿里云OSS存储
MySQL——数据库备份上传到阿里云OSS存储
20 0
|
1天前
|
SQL 存储 关系型数据库
数据库-MySQL-01(一)
数据库-MySQL-01(一)
12 4
|
1天前
|
SQL 存储 关系型数据库
数据库-MySQL-03
数据库-MySQL-03
7 0

热门文章

最新文章