数据库小项目

来源:互联网 发布:中国产业数据网 编辑:程序博客网 时间:2024/04/30 00:34
package cn.myfirstproject;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;public class MyFirstProject {public static Connection getconnect() {//获取mysql数据库的数据!Connection con = null;try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sheng", "root", "root");//System.out.println(con.isClosed());} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}public HashMap chanpin() {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;HashMap<String,String> map = new HashMap<String,String>();try {conn = MyFirstProject.getconnect();ps = conn.prepareStatement("select * from firstmysql ;");rs = ps.executeQuery();while (rs.next()) {map.put(rs.getString(1), rs.getString(2));}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (rs != null)rs.close();if (ps != null)ps.close();if (conn != null)conn.close();} catch (Exception e2) {e2.printStackTrace();}}return map;}}

package cn.myfirstproject;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;public class MyFirstOrical {public static Connection getconnect() {//获取orical的数据库数据!Connection con = null;try {Class.forName("oracle.jdbc.driver.OracleDriver");con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.15.59:1521:orcl", "C##msuser02", "Password1");//System.out.println(con.isClosed());} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return con;}public HashMap shangdian() {Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;HashMap<String,String> map = new HashMap<String,String>();try {conn = MyFirstOrical.getconnect();ps = conn.prepareStatement("select * from four_stock");rs = ps.executeQuery();while (rs.next()) {map.put(rs.getString(1), rs.getString(2));}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (rs != null)rs.close();if (ps != null)ps.close();if (conn != null)conn.close();} catch (Exception e2) {e2.printStackTrace();}}return map;}}

package cn.myfirstproject;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;import java.util.Set;public class Compare {public static void compare() throws IOException{HashMap<String,String>  orical = new MyFirstOrical().shangdian() ;HashMap<String,String>  mysql = new MyFirstProject().chanpin();StringBuilder sb = new StringBuilder();//获取键值!Set<String> mysqlset = mysql.keySet();//通过键值获取mysql表和orical表的相对应的值!for (String key : mysqlset) {String value1 = (String) mysql.get(key);String value2 = (String) orical.get(key);if(!value1 .equals(value2)){System.out.println("产品"+key+"从库存"+mysql.get(key)+",订单系统库存为"+orical.get(key));sb.append("商品ID:" +  key + ",仓库库存为:" + value1+ ",订单系统库存为" + value2 );sb.append("\r\n");MyUpdate.myupdate(key, value1);}}method(sb.toString());}public static void method(String str){FileWriter file = null;try { file = new FileWriter("F:/test/A.txt");file.write(str);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {file.flush();file.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

package cn.myfirstproject;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class MyUpdate {public static void myupdate(String key , String values) {Connection con = null;PreparedStatement pre = null;ResultSet  rs = null;try {Class.forName("oracle.jdbc.driver.OracleDriver");con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.15.59:1521:orcl", "C##msuser02", "Password1");pre = con.prepareStatement("update four_stock set product_inventory=? where product_id=?");pre.setString(1, values);pre.setString(2, key);pre.executeQuery();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

package cn.myfirstproject;import java.io.IOException;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class MyFirstProjectTest {public static void main(String[] args) throws IOException {Timer timer = new Timer();TimerTask task = new TimerTask() {@Overridepublic void run() {try {Compare.compare();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};timer.scheduleAtFixedRate(task, new Date(), 2000);}}
通过此次小项目·的实操,我发现了自己在知识点的操作运用上还不是很熟练,不过整个项目做下来感觉收货还是挺大的!
0 0
原创粉丝点击