码迷,mamicode.com
首页 > 数据库 > 详细

SQL练习

时间:2017-08-19 22:18:38      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:values   blog   integer   jdb   val   技术   drive   menu   修改   

记得导包技术分享

/class TestUser

package com.JDBCsql;

import lombok.Data;

@Data
public class TestUser {
    private Integer id;
    private String name;
    private Integer pasword;
    private String mail;
    private String root;
}

  /class TestMysql

package com.JDBCsql;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class TestMysql {
       private static String DRIVER=null;
       private static String URL=null;
       private static  String USERNAME = null;   	
   	   private static  String PASSWORD = null;
   	static{
		try {
			FileInputStream fis 
			= new FileInputStream("src/db.properties");
			Properties pt = new Properties();
			pt.load(fis);
			DRIVER = pt.getProperty("DRIVER");
			URL = pt.getProperty("URL");
			USERNAME = pt.getProperty("USERNAME");
			PASSWORD = pt.getProperty("PASSWORD");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
   	public static Connection getConn(){
		Connection conn=null;
		try {
			/*加载驱动*/
//			System.out.println(DRIVER);
			Class.forName(DRIVER);
			/*连接数据库*/
			conn= DriverManager.getConnection(URL, USERNAME, PASSWORD);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	//关闭连接
	public static void close(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//关闭连接
	public static void close(PreparedStatement ps){
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//关闭连接
	public static void close(ResultSet rs){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public static int executeUpdate(String sql,Object... papram) {
		// TODO Auto-generated method stub
		Connection conn =getConn();
		int result=0;
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			if (papram!=null) {
				for (int i = 0; i < papram.length; i++) {
					pst.setObject(i+1, papram[i]);
				}
			}
			result =pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(conn);
		}
		return result;

	}
	
}

  /class TestMenu

package com.JDBCsql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestMenu {

     public static int show(Scanner scanner,TestUser testUser1,String a4) {
    	System.out.println("查询全部用户-----------------1");
 		System.out.println("根据ID查询用户………………………………………2");
 		System.out.println("根据姓名查询用户--------------3");
 		System.out.println("请输入要做的操作:");
 		int d=scanner.nextInt();
 		switch (d) {
		case 1:
			getUser1(testUser1);
			
			RootUser.show(a4, scanner, testUser1);
			break;
        case 2:
        	
        	getUser2(testUser1, scanner);
        	System.out.println(testUser1.toString());
        	System.out.println("=========================================");
			RootUser.show(a4, scanner, testUser1);
			break;
        case 3:
        	getUser3(testUser1, scanner);
        	System.out.println(testUser1.toString());
			System.out.println("=========================================");
			RootUser.show(a4, scanner, testUser1);
			break;
		default:
			break;
		}
		return d;
	}
     public static int getUser1(TestUser testUser1){
 		Connection conn = 	TestMysql.getConn();
 		List<TestUser>list=new ArrayList<TestUser>();
 		ResultSet index ;//记录行数
 		PreparedStatement pst = null;
 		String sql =  "select * from user ";		
 		try {
 			pst = conn.prepareStatement(sql);
 			
 			index = pst.executeQuery();
 			while (index.next()) {
 				TestUser testUser=new TestUser();
 				testUser.setId(index.getInt("id"));
 				testUser.setName(index.getString("name"));
 				testUser.setPasword(index.getInt("pasword"));
 				testUser.setMail(index.getString("mail"));
 				testUser.setRoot(index.getString("root"));
 				list.add(testUser);
 			}	
 			
 			for (TestUser testUser2 : list) {
				System.out.println(testUser2.getId()+" "+testUser2.getName()+" "+testUser2.getPasword()+" "+testUser2.getMail()+" "+testUser2.getRoot());
				System.out.println("=========================================");
			}
 		} catch (SQLException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}finally {
 			try {
 				pst.close();
 				conn.close();
 			} catch (SQLException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			
 		}
		return 0;
 		
 	}
     public static int getUser2(TestUser testUser1,Scanner scanner){
 		Connection conn = 	TestMysql.getConn();		
 		ResultSet index ;//记录行数
 		PreparedStatement pst = null;
 		System.out.println("请输入要查询的ID:");
    	String s=scanner.next();
    	testUser1.setId(Integer.valueOf(s));
 		String sql =  "select * from user where id=‘"+testUser1.getId()+"‘";		
 		try {
 			pst = conn.prepareStatement(sql);
 			
 			index = pst.executeQuery();
 			while (index.next()) {
 				testUser1.setId(index.getInt("id"));
 				testUser1.setName(index.getString("name"));
 				testUser1.setPasword(index.getInt("pasword"));
 				testUser1.setMail(index.getString("mail"));
 				testUser1.setRoot(index.getString("root"));
 			}
 		} catch (SQLException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}finally {
 			try {
 				pst.close();
 				conn.close();
 			} catch (SQLException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			
 		}
		return 0;
 		
 	}
     public static int getUser3(TestUser testUser1,Scanner scanner){
 		Connection conn = 	TestMysql.getConn();		
 		ResultSet index ;//记录行数
 		System.out.println("请输入要查询的用户名:");
 		String s=scanner.next();
 		testUser1.setName(s);
 		PreparedStatement pst = null;
 		String sql =  "select * from user where name=‘"+testUser1.getName()+"‘";		
 		try {
 			pst = conn.prepareStatement(sql);
 			
 			index = pst.executeQuery();
 			while (index.next()) {
 				testUser1.setId(index.getInt("id"));
 				testUser1.setName(index.getString("name"));
 				testUser1.setPasword(index.getInt("pasword"));
 				testUser1.setMail(index.getString("mail"));
 				testUser1.setRoot(index.getString("root"));
 			}	
 		} catch (SQLException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}finally {
 			try {
 				pst.close();
 				conn.close();
 			} catch (SQLException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 			
 		}
 		return 0;	
 	}
}

  /class PublicUser

package com.JDBCsql;

import java.util.Scanner;
import com.JDBCsql.TestUser;
public class PublicUser {

	public static int show(String a4,Scanner scanner,TestUser testUser1){
    	System.out.println("登录成功。。。。");
		System.out.println("======================");
		System.out.println("欢迎登录主窗体");
		System.out.println(a4+"您好"+"    "+"您的权限是"+testUser1.getRoot());
		System.out.println("======================");
		System.out.println("修改自己的信息---------------1");
		System.out.println("查询自己的信息………………………………………2");
		System.out.println("退出主程序------------------3");
		int d=scanner.nextInt();
		switch (d) {
		case 1:
			System.out.println("请输入您的新用户名:");
			String q=scanner.next();
			
			testUser1.setName(q);
			System.out.println("请输入您的新密码:");
			String q1=scanner.next();
			testUser1.setPasword(Integer.valueOf(q1));
			System.out.println("请输入您的新邮箱:");
			String q3=scanner.next();
			testUser1.setMail(q3);
		    testUser1.getId();
			update1(testUser1);
			System.out.println("信息修改成功!");
			PublicUser.show(a4, scanner, testUser1);
			break;
			
		case 2:
			System.out.println("您的用户名为:"+testUser1.getName()+"您的用户密码是:"+testUser1.getPasword()+"您的邮箱是:"+testUser1.getMail()+"您的权限是:"+testUser1.getRoot());
			PublicUser.show(a4, scanner, testUser1);
			break;
        
		default:
			break;
		}
		return d;     	
    }

	private static int update1(TestUser testUser1) {
		// TODO Auto-generated method stub
		return TestMysql.executeUpdate("update user set name=?,pasword=?,mail=?where id=?",testUser1.getName(),testUser1.getPasword(),testUser1.getMail(),testUser1.getId());
	}
}

  /class RootUser

package com.JDBCsql;

import java.util.Scanner;

public class RootUser {
	public static int show(String a4,Scanner scanner,TestUser testUser1){
    	System.out.println("登录成功。。。。");
		System.out.println("======================");
		System.out.println("欢迎登录主窗体");
		System.out.println(a4+"您好"+"    "+"您的权限是"+"管理员");
		System.out.println("======================");
		System.out.println("添加用户---------------1");
		System.out.println("删除用户………………………………………2");
		System.out.println("修改用户---------------3");
		System.out.println("查询用户………………………………………4");
		System.out.println("退出程序---------------5");
		int d=scanner.nextInt();
		switch (d) {
		case 1:
			System.out.println("请输入您想要创建的用户名:");
			String q=scanner.next();
			testUser1.setName(q);
			System.out.println("请输入您想要创建的用户密码:");
			String q1=scanner.next();
			testUser1.setPasword(Integer.valueOf(q1));
			System.out.println("请输入新用户的邮箱:");
			String q3=scanner.next();
			testUser1.setMail(q3);
			String q4="管理员";
			testUser1.setRoot(q4);
			add(testUser1);
			System.out.println("新用户创建成功!");
			RootUser.show(a4, scanner, testUser1);
			break;
			
		case 4:
			TestMenu.show(scanner, testUser1, a4);
//			RootUser.show(a4, scanner, testUser1);
			break;
        case 2:
        	System.out.println("请输入您想要删除用户的ID:");
			String q5=scanner.next();
			testUser1.setId(Integer.valueOf(q5));
			delete(Integer.valueOf(q5));
			System.out.println("用户删除成功!");
			RootUser.show(a4, scanner, testUser1);
        	break;
        case 3:
        	System.out.println("请输入您想要修改的用户ID:");
			String v=scanner.next();
			testUser1.setId(Integer.valueOf(v));
        	System.out.println("请输入您想要修改的用户名:");
			String v1=scanner.next();
			testUser1.setName(v1);
			System.out.println("请输入您想要修改的用户密码:");
			String v2=scanner.next();
			testUser1.setPasword(Integer.valueOf(v2));
			System.out.println("请输入您想要修改的用户的邮箱:");
			String v3=scanner.next();
			testUser1.setMail(v3);
			System.out.println("请输入您想要修改的用户的权限(管理员/普通用户):");
			String v4=scanner.next();
			testUser1.setRoot(v4);
			update(testUser1);
			System.out.println("新用户创建成功!");
			RootUser.show(a4, scanner, testUser1);
        	break;
		default:
			break;
		}
		return d;     	
    }

	private static int update(TestUser testUser1) {
		// TODO Auto-generated method stub
		return TestMysql.executeUpdate("update user set name=?,pasword=?,mail=?,root=? where id=?",testUser1.getName(),testUser1.getPasword(),testUser1.getMail(),testUser1.getRoot(),testUser1.getId());
	}

	private static int delete(Integer q5) {
		// TODO Auto-generated method stub
		return TestMysql.executeUpdate("delete from user where id=?",q5);
	}

	private static int add(TestUser testUser1) {
		// TODO Auto-generated method stub
		return TestMysql.executeUpdate("insert into user (name,pasword,mail,root) values (?,?,?,?)",testUser1.getName(),testUser1.getPasword(),testUser1.getMail(),testUser1.getRoot());
	}

	
}

  /class TestView

package com.JDBCsql;

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

public class TestView {
	public static int delete(int id){
		return TestMysql.executeUpdate("delete from user where id=?",id);
		
	}
	public static int update(TestUser testUser){
		return TestMysql.executeUpdate("update user set name=?,pasword=?,mail=?,root=? where id=?",testUser.getName(),testUser.getPasword(),testUser.getMail(),testUser.getRoot(),testUser.getId());
	}
	public static int update1(TestUser testUser){
		return TestMysql.executeUpdate("update user set name=?,pasword=?,mail=?where id=?",testUser.getName(),testUser.getPasword(),testUser.getMail(),testUser.getId());
	}
	public static int add(TestUser testUser){
		return TestMysql.executeUpdate("insert into user (name,pasword,mail,root) values (?,?,?,?)",testUser.getName(),testUser.getPasword(),testUser.getMail(),testUser.getRoot());
	}
	public static TestUser getUser(TestUser testUser){
		Connection conn = 	TestMysql.getConn();		
		TestUser testUser4 =new TestUser();
		ResultSet index ;//
		PreparedStatement pst = null;
		String sql =  "select * from user where name=‘"+testUser.getName()+"‘ and pasword=‘"+testUser.getPasword()+"‘";		
		try {
			pst = conn.prepareStatement(sql);
			
			index = pst.executeQuery();
			if (index.next()==false) {
				return new TestUser();
			}
			else {
				testUser4.setId(index.getInt("id"));
				testUser4.setName(index.getString("name"));
				testUser4.setPasword(index.getInt("pasword"));
				testUser4.setMail(index.getString("mail"));
				testUser4.setRoot(index.getString("root"));
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				pst.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return testUser4;	
	}
	public static void main(String[] args) {
		System.out.println("欢迎使用neusoft用户管理系统");
		System.out.println("======================");
		System.out.println("用户注册----------------1");
		System.out.println("用户登录----------------2");
		System.out.println("退出系统----------------3");
		Scanner scanner=new Scanner(System.in);
		int key = scanner.nextInt();
		switch (key) {
		case 2:
			System.out.println("欢迎");
			System.out.println("======================");
			System.out.println("");
			String a1=scanner.next();
			TestUser testUser =new TestUser();
			testUser.setName(a1);
			System.out.println("输入用户名");
			String a2=scanner.next();
			testUser.setPasword(Integer.valueOf(a2));
			System.out.println("输入邮箱");
			String a3=scanner.next();
			testUser.setMail(a3);
			System.out.println("输入权限");
			String a7=scanner.next();
			testUser.setRoot(a7);
			add(testUser);
			System.out.println("");
			TestView.main(args);
			break;
		case 1:
			System.out.println("");
			System.out.println("======================");
			System.out.println("");
			String a4=scanner.next();
			TestUser testUser1 =new TestUser();
			testUser1.setName(a4);
			System.out.println("?");
			int a5=scanner.nextInt();
			testUser1.setPasword(a5);
			testUser1=TestView.getUser(testUser1);
			if (testUser1==null) {
				System.out.println("");
			} else if (testUser1.getRoot().equals("")==true) {
				PublicUser.show(a4, scanner, testUser1);
			}else  {
				RootUser.show(a4, scanner, testUser1);
			}

			break;

		default:
			scanner.close();
			break;
		}
	}
}

  

SQL练习

标签:values   blog   integer   jdb   val   技术   drive   menu   修改   

原文地址:http://www.cnblogs.com/lc-java/p/7397882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!