数据库的增删改查加遍历

一、jar包的导入,和相关类的创建:
二、创建数据库,在其中创建表。
三、功能选择程序,主要引用method类中的方法:
package com.cn.database;

import java.sql.SQLException;
import java.util.Scanner;


public class start {
	Method m  =new Method();
	public void reset(){
		
		Scanner in = new Scanner(System.in);
		System.out.println("请选择功能:");
		System.out.println("1.查询 2.删除3.遍历数据库信息 4.增加 5.改数据");
		
		int i = in.nextInt();
		if(i>5||i<1){
			System.out.println("您输出的数值超出有效数值,请重新输入!!!!");
		}
		switch(i){
		case  1:
			try {
				m.find();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			break;	
		case  2:
			try {
				m.delete();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			break;
		case  3:
			try {
				m.ergodic();
			} catch (ClassNotFoundException e) {
				
				e.printStackTrace();
				
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			break;	
		case  4:
			try {
				m.insert();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			break;	
		case  5:
			try {
				m.change();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			break;	
			
		}
	}
	public static void main(String args[]) throws SQLException, ClassNotFoundException{
		start s =new start();
		for(int i=0;i<1;i--){
			s.reset();
		}
	
	
	}
}
4、method类,实现加载驱动和数据库的连接,五个方法分别是数据库的增删改查加遍历(顺序自己对号入座*——*)
package com.cn.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;



public class Method {
	Connection con ;

PreparedStatement pst=null;
ResultSet rs = null;
	
	String Driver="com.mysql.jdbc.Driver";//创建加载驱动
	String Url="jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8";//创建数据库连接
	String userName="root";//数据库用户名
	String pwd="123456";//数据库密码
	//安装时我的数据空权限是可以不用密码访问,所以这两句可写可不写,但是印在危险不多说,我有点怕
	
	Scanner in = new Scanner(System.in);
	public void find() throws SQLException{//查找方法

		try {
			

			System.out.println("请输入名字:");
			String name = in.nextLine();
			String sql="select * from studentinfo where stuname=?";//查的sql语句 
			Class.forName(Driver);加载驱动
			con=DriverManager.getConnection(Url,userName,pwd);//加载数据库
			
			pst=con.prepareStatement(sql);
			pst.setString(1,name);//从第一行开始匹配 我们要找的内容
			rs=pst.executeQuery();//执行sql语句,rs为查找的对象
			while (rs.next()) {//对查找的结果遍历
				String stuname = rs.getString("stuname");
				String stumajor = rs.getString("stumajor");
				String stugrade = rs.getString("stugrade");
				String studirection = rs.getString("studirection");
				System.out.println("______________________________________________");
				System.out.println(stuname+" "+stumajor+" "+stugrade+" "+studirection);
				System.out.println("______________________________________________");
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}finally{
			rs.close();
			pst.close();
			con.close();//数据库的三个关闭流,关闭的顺序是从下往上关;
		}
		
	
	}
	public void delete()throws SQLException{//删除的方法
		ResultSet rs1 = null;
		PreparedStatement pst1 = null;
		try{
			
			System.out.println("请输入名字:");
			String name = in.nextLine();
			
			
			
			Class.forName(Driver);
		con=DriverManager.getConnection(Url,userName,pwd);
		String sql="delete  from studentinfo where stuname=?";//删除的sql语句
		pst=con.prepareStatement(sql);
		pst.setString(1, name);
		pst.executeUpdate();
		System.out.println("______________________________________________");
		System.out.println("恭喜你,删除成功!");
		System.out.println("______________________________________________");
	
		pst.close();
		con.close();
		String sql1="select * from studentinfo ";//遍历数据库
		Class.forName(Driver);
		con=DriverManager.getConnection(Url,userName,pwd);
		pst1=con.prepareStatement(sql1);
		rs1=pst1.executeQuery();
		System.out.println("删除后的表的信息为:");
		System.out.println("______________________________________________");
		while (rs1.next()) {
			String stuname = rs1.getString("stuname");
			String stumajor = rs1.getString("stumajor");
			String stugrade = rs1.getString("stugrade");
			String studirection = rs1.getString("studirection");
			System.out.println(stuname+""+stumajor+""+stugrade+""+studirection);
		}
		System.out.println("______________________________________________");
	}catch(ClassNotFoundException e){
		e.printStackTrace();
	}finally{
		rs1.close();
		pst.close();
		con.close();
	}
		}
	public void change()throws SQLException{//改的方法
		ResultSet rs1=null;
		try{
			System.out.println("请输入名字:");
			String name = in.nextLine();
			
			
		System.out.println("请输入修改的内容:");
		String sql="update studentinfo SET stuname = ? ,stumajor =?, stugrade = ?,studirection = ? where stuname = ?";//改的sql语句
		

		con=DriverManager.getConnection(Url,userName,pwd);
		pst=con.prepareStatement(sql);
		System.out.println("name:");
		String name1 = in.next();
		System.out.println("major:");
		String major = in.next();
		System.out.println("grade:");
		String grade = in.next();
		System.out.println("direction:");
		String direction = in.next();//输入流,输入要插入的信息
		
		pst.setString(1, name1);
		pst.setString(2, major);
		pst.setString(3,grade);
		pst.setString(4, direction);
		pst.setString(5, name);
		pst.executeUpdate();
		System.out.println("您更改后的信息为:"+name1+"  "+major+"  "+grade+"  "+direction);
		con.close();
		System.out.println("______________________________________________");
		System.out.println("更改后表的信息为:");
		System.out.println("______________________________________________");
		
		String sql1="select * from studentinfo";
		con=DriverManager.getConnection(Url,userName,pwd);
		pst=con.prepareStatement(sql1);
		 rs1=pst.executeQuery();
		while(rs1.next()){
			String stuname = rs1.getString("stuname");
			String stumajor = rs1.getString("stumajor");
			String stugrade = rs1.getString("stugrade");
			String studirection = rs1.getString("studirection");
			System.out.println(stuname+"  "+stumajor+"  "+stugrade+"  "+studirection);
		}
		System.out.println("______________________________________________");
		
		}finally{
			rs1.close();
			pst.close();
			con.close();
		}
	}
	public void insert()throws SQLException{//插入的方法
		ResultSet rs1 = null;
		try{
			System.out.println("请输入你要插入人的信息:");
			String sql="insert into  studentinfo values(?,?,?,?)";//插入的sql语句
			Class.forName(Driver);

		con=DriverManager.getConnection(Url,userName,pwd);
		pst=con.prepareStatement(sql);
		System.out.println("name:");
		String name = in.next();
		System.out.println("major:");
		String major = in.next();
		System.out.println("grade:");
		String grade = in.next();
		System.out.println("direction:");
		String direction = in.next();
		
		pst.setString(1, name);
		pst.setString(2, major);
		pst.setString(3,grade);
		pst.setString(4, direction);
	
		pst.executeUpdate();	
		System.out.println("______________________________________________");
		System.out.println("你插入的信息为:");
		System.out.println(name+"  "+major+"  "+grade+"  "+direction);
		System.out.println("______________________________________________");
		String sql2="select * from studentinfo ";
		pst=con.prepareStatement(sql2);
		 rs1=pst.executeQuery();
		while (rs1.next()) {
			String stuname = rs1.getString("stuname");
			String stumajor = rs1.getString("stumajor");
			String stugrade = rs1.getString("stugrade");
			String studirection = rs1.getString("studirection");
			System.out.println(stuname+"  "+stumajor+"  "+stugrade+"  "+studirection);
		}
		
	}catch(ClassNotFoundException e){
		e.printStackTrace();
	}finally{
		rs1.close();
		pst.close();
		con.close();
		}
	}
	public void ergodic()throws SQLException, ClassNotFoundException{//数据库的遍历
		String sql="select * from studentinfo ";
		Class.forName(Driver);
		con=DriverManager.getConnection(Url,userName,pwd);
		pst=con.prepareStatement(sql);
		rs=pst.executeQuery();
		System.out.println("现在的表单内容有:");
		System.out.println("______________________________________________");
		while(rs.next()){
			String stuname = rs.getString("stuname");
			String stumajor = rs.getString("stumajor");
			String stugrade = rs.getString("stugrade");
			String studirection = rs.getString("studirection");
			System.out.println(stuname+"  "+stumajor+"  "+stugrade+"  "+studirection);
		}
		System.out.println("______________________________________________");
		rs.close();
		pst.close();
		con.close();
	}
	
}



第一次写数据库的程序,作为刚学了三天的数据库的小白,可能有很多错误,希望大能多多指教!!!!!1





你可能感兴趣的:(数据库的增删改查加遍历)