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;
}
}
}
}