1.方法类
package com.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class tool {
/**
* @throws Exception
*/
static Connection conn =null;
static Statement st =null;
static ResultSet rs =null;
public static void mains() {
try{
Class.forName("oracle.jdbc.OracleDriver");
conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","mysql","123456");
st=conn.createStatement();
rs=st.executeQuery("select numno,ename from dept");
while (rs.next()) {
System.out.println(rs.getInt("numno") +"---"+ rs.getString("ename"));
}
}catch (Exception e) {
e.printStackTrace();
}/*finally {
try {
if (rs != null)
rs.close();
if (st != null)
st.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
}
//添加数据
public static void add(String num1, String name1) throws SQLException{
int addRe=st.executeUpdate("insert into dept (numno,ename) values ("+num1+",‘"+name1+"‘)");
System.out.println("******添加操作结果是:"+addRe);
}
//删除数据
public static void delete(String num2) throws SQLException{
int dele=st.executeUpdate("delete from dept where numno="+num2+"");
System.out.println("******删除操作结果是:"+dele);
}
//修改数据
public static void alters(String num3, String name2) throws SQLException{
int alt=st.executeUpdate("update dept set ename=‘"+name2+ "‘where numno="+num3+"");
System.out.println("******删除操作结果是:"+alt);
}
}
2.调用类(执行类,测试类)
import java.sql.SQLException;
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println(" 1.添加 2.删除 3.修改 4.退出 ");
int num = 0;
try {
num = sca.nextInt();
} catch (Exception e) {
System.out.println("异常");
break;
}
switch (num) {
case 1:
System.out.println("添加前的数据");
tool.mains();
System.out.println("请输入您添加的编号:");
String num1 = sca.next();
//保证输入的是数字
for (int i = num1.length(); --i >= 0;) {
int nums = num1.charAt(i);
if (num1.length() == 4 && nums >= 48 && nums <= 57) {
System.out.println("请输入您添加的名称:");
String name1 = sca.next();
try {
tool.add(num1, name1);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("添加后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}
break;
//删除数据
case 2:
System.out.println("删除前的数据");
tool.mains();
System.out.println("请输入您删除的编号:");
String num2 = sca.next();
//数字
for (int i = num2.length(); --i >= 0;) {
int nums1 = num2.charAt(i);
if (num2.length() == 4 && nums1 >= 48 && nums1 <= 57) {
try {
tool.delete(num2);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("删除后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}
break;
case 3:
//修改数据
System.out.println("修改前的数据");
tool.mains();
System.out.println("请输入您修改的编号:");
String num3 = sca.next();
//数字
for (int i = num3.length(); --i >= 0;) {
int nums2 = num3.charAt(i);
if (num3.length() == 4 && nums2 >= 48 && nums2 <= 57) {
System.out.println("请输入您修改的名称:");
String name2 = sca.next();
try {
tool.alters(num3, name2);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("修改后的数据--");
tool.mains();
i=-i;
} else {
System.out.println("输入内容有误,请输入4位数数字");
break;
}
}
break;
case 4:
System.out.println("您已退出系统!");
flag = false;
break;
default:
System.out.println("请输入规定的数字!");
break;
}
}
}
}