Java每日一题24(最后一题)

昨天又写了个双色球程序,作为Java每日一题系列的终止符:
Java链接derby数据库,我将2003年至今的数据导入到了数据库中,可以对数据进行CRUD,最大的好处是,比如输入03 05 07,可以查到2003年至今这三个号分别在哪期出现过。
但是我觉得自己写的太拖沓,大家看看有哪些地方可以重构下,呵呵

我把项目打成jar包作为附件了,不过很奇怪,打成包以后,就找不到jdbc驱动了,了解这个和问题的朋友麻烦告知,万分感谢!!!

[color=red]
liangwenzheng 写道
敬请博主放过JAVA初级程序员!别再出这些题目了,会把人引入歧途的~结果只有一个,造就一批适用JAVA程序设计语言写C程序的人!
我好心劝你一句而已,你却和我抬杠,呵呵~
初学者走的路很重要,要是一开始就被带沟里去了,后患无穷。
本着 对他人做点善事的思想 再次拜请博主三思~
PS:别拿无知当个性!
[/color]
[color=blue]
为了不把一些高手带到沟里去,我决定停止出题,感谢大家多日以来的关注和支持,在此谢过,我会继续写其他方面的blog,Java,Python,Oracle,DB2,Linux,AIX,欢迎大家MSN交流:[email protected][/color]


import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Properties;
import java.util.Scanner;

public class DoubleBall {

private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String dbName = "/DoubleBall";
private String dbURL = "jdbc:derby:" + dbName;

private Connection conn = null;
private Statement st = null;
private ResultSet rs = null;

public static void main(String[] args) {
Properties props = System.getProperties();
String extdir = props.getProperty("java.ext.dirs");
extdir = extdir + File.pathSeparator + "lib\\derby.jar";

while (true) {
System.out.println("What do you want to do?");
System.out.println("1.Create Data");
System.out.println("2.Delete Data");
System.out.println("3.Update Data");
System.out.println("4.Retrieve Data");
System.out.println("5.Display All Data");
System.out.println("6.Exit");
Scanner scanner = new Scanner(System.in);
int selected = scanner.nextInt();

DoubleBall doubleBall = new DoubleBall();
if (selected == 1) {
doubleBall.CreateData();
} else if (selected == 2) {
doubleBall.DeleteData();
} else if (selected == 3) {
doubleBall.UpdateData();
} else if (selected == 4) {
doubleBall.RetriveData();
} else if (selected == 5) {
doubleBall.DisplayAll();
} else {
System.exit(selected);
}
}
}

private void getConn() throws Exception {
Class.forName(driver);
conn = DriverManager.getConnection(dbURL);
st = conn.createStatement();
}

private void closeConn() throws Exception {
if (rs != null) {
rs.close();
}
st.close();
conn.close();
}

public void DisplayAll() {
try {
getConn();
rs = st.executeQuery("select * from DATA");
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
df.applyPattern("00");
while (rs.next()) {
int id = rs.getInt(1);
int red_1 = rs.getInt(2);
int red_2 = rs.getInt(3);
int red_3 = rs.getInt(4);
int red_4 = rs.getInt(5);
int red_5 = rs.getInt(6);
int red_6 = rs.getInt(7);
int blue = rs.getInt(8);

System.out.println(id + " " + df.format(red_1) + " "
+ df.format(red_2) + " " + df.format(red_3) + " "
+ df.format(red_4) + " " + df.format(red_5) + " "
+ df.format(red_6) + " " + df.format(blue));

}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
closeConn();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void CreateData() {
System.out
.println("Please input the data like this:2009024 01 03 17 23 30 33 12");
Scanner scanner = new Scanner(System.in);
String[] temp = scanner.nextLine().split(" ");

try {
getConn();
st.executeUpdate("insert into DATA values(" + temp[0] + ","
+ temp[1] + "," + temp[2] + "," + temp[3] + "," + temp[4]
+ "," + temp[5] + "," + temp[6] + "," + temp[7] + ")");
System.out.println("Input Successful!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
closeConn();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void RetriveData() {
System.out.println("Please input the number,like this:05 07");
Scanner scanner = new Scanner(System.in);
String[] temp = scanner.nextLine().split(" ");
try {
getConn();
rs = st.executeQuery("select * from DATA");
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
df.applyPattern("00");
int count = 0;
System.out.println("======================================");
while (rs.next()) {
int[] balls = new int[7];
int id = rs.getInt(1);
for (int i = 0; i < 7; i++) {
balls[i] = rs.getInt(i + 2);
}

String data = df.format(balls[0]) + " " + df.format(balls[1])
+ " " + df.format(balls[2]) + " " + df.format(balls[3])
+ " " + df.format(balls[4]) + " " + df.format(balls[5]);

String regex = "";
for (String s : temp) {
regex = regex + ".*" + s;
}
regex = regex + ".*";

if (data.matches(regex)) {
count++;
System.out.println(id + " " + df.format(balls[0]) + " "
+ df.format(balls[1]) + " " + df.format(balls[2])
+ " " + df.format(balls[3]) + " "
+ df.format(balls[4]) + " " + df.format(balls[5])
+ " " + df.format(balls[6]));
}
}
System.out.println("======================================");
System.out.println("From 20030101 to now: It comes " + count+" times!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
closeConn();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void UpdateData() {
System.out
.println("Please input the data ID which you want to update,like this:2009024 01 03 17 23 30 33 12");
Scanner scanner = new Scanner(System.in);
String[] temp = scanner.nextLine().split(" ");

try {
getConn();
st.executeUpdate("update DATA set RED_1=" + temp[1] + ",RED_2="
+ temp[2] + ",RED_3=" + temp[3] + ",RED_4=" + temp[4]
+ ",RED_5=" + temp[5] + ",RED_6=" + temp[6] + ",BLUE="
+ temp[7] + " where ID=" + temp[0]);
System.out.println("Update Successful!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
closeConn();
} catch (Exception e) {
e.printStackTrace();
}
}
}

public void DeleteData() {
System.out
.println("Please input the data ID which you want to detete,like this:2009024");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
try {
getConn();
st.executeUpdate("delete from DATA where ID=" + temp);
System.out.println("Delete Successful!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
closeConn();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

你可能感兴趣的:(Java,Tips,Java,Derby,AIX,SQL,编程)