栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA与Mysql.

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
//遍历集合
		
	}

}

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;
	}
	
	public int getStuId() {
		return stuId;
	}
	
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	
	public String getStuName() {
		return stuName;
	}
	
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	
	public int getStuAge() {
		return stuAge;
	}
	
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	@Override
	public String toString() {
		return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuSex=" + stuSex + ", stuAge=" + stuAge + "]";
	}
	
	

}

 

 

package Code;

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:UsersMZFAITHDREAMDesktop1.jpg (系统找不到指定的文件。)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.(FileInputStream.java:138)
	at java.io.FileInputStream.(FileInputStream.java:93)
	at repeat.Copy1.main(Copy1.java:13)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/582015.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号