⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 employee1.java

📁 用于连接数据库的简单JAVA程序,适合初学者了解.
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;

public class Employee1 
{	
	DataInputStream data;
	String i, n, a, d;
	int s;
	public void input()
	{
		data = new DataInputStream(System.in);
		try{
			System.out.println("Enter the ID of an Employee");
			i = data.readLine();
			System.out.println("Enter the Name of an Employee");
			n= data.readLine();
			System.out.println("Enter the Age of an Employee");
			a = data.readLine();
			System.out.println("Enter the Salary of an Employee");
			s= Integer.parseInt(data.readLine());
			System.out.println("Enter the Designation of an Employee");
			d= data.readLine();
		}catch(Exception e){}
	
	try
	{
	Connection con=DriverManager.getConnection("jdbc:odbc:test1");
	Statement st=con.createStatement();
	int r=st.executeUpdate("insert into test1 values(' "+i+" ', ' "+n+" ', ' "+a+" ', ' "+s+" ', ' "+d+" ')");
	//JOptionPane.showMessageDialog(null,"Record Updated", "Successful", JOptionPane.INFORMATION_MESSAGE);
	System.out.println("\n\n\n************Record Updated***********");
	con.close();
	}catch(SQLException e)
	{
			System.out.println(e);
	}
	}
	
	public void show()
	{
		try
		{
			Connection con=DriverManager.getConnection("jdbc:odbc:test1");
			Statement st=con.createStatement();
			ResultSet rs = st.executeQuery("Select * from test1");
			System.out.println("\n\n********The Record are as follows :*********");
			while(rs.next())
			{
				System.out.print(rs.getString(1)+"\t");	
				System.out.print(rs.getString(2)+"\t");
				System.out.print(rs.getString(3)+"\t");
				System.out.print(rs.getInt(4)+"\t");
				System.out.println(rs.getString(5));
			}
			con.close();
		}
		catch(SQLException e)
		{
			System.out.println(e);
		}
	}
	
	public void delete()
	{	
		data = new DataInputStream(System.in);
		String del =" ";
		
		try{
		System.out.println("Enter the Employee ID , whose record you want to delete");
		del = data.readLine();
		
		}catch(Exception e){}
		try
		{
			Connection con=DriverManager.getConnection("jdbc:odbc:test1");
			Statement st=con.createStatement();
			int i = st.executeUpdate("Delete from test1 where ID =' "+del+" ' ");
			System.out.println("\n\n\n*********Record Deleted*********");
		}
		catch(SQLException e)
		{
			System.out.println(e);
		}
	}
	public void search()
	{
		data = new DataInputStream(System.in);
		String s = " ";
		
		try{
		System.out.println("Enter the Employee ID, whom u want to search");
		s= data.readLine();
		}catch(Exception e){}
		
		try
		{
			Connection con=DriverManager.getConnection("jdbc:odbc:test1");
			Statement st=con.createStatement();
			ResultSet rs= st.executeQuery("Select * from test1 where ID =' "+s+" ' ");
			
			System.out.println("\n\n*************The record is as follows:**********");
			while(rs.next())
			{
				System.out.print(rs.getString(1)+"\t");	
				System.out.print(rs.getString(2)+"\t");
				System.out.print(rs.getString(3)+"\t");
				System.out.print(rs.getInt(4)+"\t");
				System.out.println(rs.getString(5));
			}

		}catch(SQLException e)
		{}
	}
	
	public static void main(String args[])
	{
		String ch=" ";
		DataInputStream data = new DataInputStream(System.in);
		try{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		}catch(ClassNotFoundException e)
		{
			System.out.println(e);
		}	
		Employee1 emp = new Employee1();
	
		do
		{
			System.out.println("1. Input the records");
			System.out.println("2. Display the records");
			System.out.println("3. Delete the record");
			System.out.println("4. Search the record");
			System.out.println("5. Close the Application");
			System.out.println("Enter your choice");
			
			try{
				int num = Integer.parseInt(data.readLine());
				switch(num)
				{
					case 1:
					emp.input();
					break;

					case 2:
					emp.show();
					break;
		
					case 3:
					emp.delete();
					break;
			
					case 4:
					emp.search();	
					break;
				
					case 5:
					System.exit(0);
					break;
		
				}
		
			System.out.println("\n\n\nDo you want to continue");
			ch = data.readLine();
			}catch(Exception e){}
		
		}while(ch.equals("y"));
		}
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -