smsdao.java.svn-base

来自「实现很多功能 实现很多功能 实现很多功能」· SVN-BASE 代码 · 共 124 行

SVN-BASE
124
字号
package com.easycolony;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SmsDao {
	public static final int ERROR_PRODUCT_CODE = 2;
	public static final int ERROR_STORE_CODE = 4;
	public static final int ERROR_BOTH_CODE = 8;
	public static final int SUCCESS = 0;
	
	
	private String url;
	private String username;
	private String password;
	
	
	
	public int addRecord(long productCode, long storeCode) {
		
		int rt = SUCCESS;
		
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			stmt = conn.prepareStatement("insert into t_data_sale_flow (product_code,store_code) values(?,?)");
			stmt.setLong(1, productCode);
			stmt.setLong(2, storeCode);
			stmt.execute();
		} catch (Exception e) {
			e.printStackTrace();
			boolean invalidProductCode = false;
			boolean invalidStoreCode = false;
			
			try{
				rs = stmt.executeQuery("select id from t_dic_product where id="+productCode);
				if(rs==null || !rs.next()){
					invalidProductCode = true;
				}
				
				rs = stmt.executeQuery("select id from t_dic_chain_store where id="+storeCode);
				if(rs==null || !rs.next()){
					invalidStoreCode = true;
				}
				
				if(invalidProductCode && invalidStoreCode){
					rt = ERROR_BOTH_CODE;
				}else if(invalidProductCode){
					rt = ERROR_PRODUCT_CODE;
				}else if(invalidStoreCode){
					rt = ERROR_STORE_CODE;
				}
				
			}catch(Exception ex1){
				
			}
			
		} finally {
			if (conn != null) {
				try {
					conn.close();
					conn = null;
				} catch (Exception e) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
					stmt = null;
				} catch (Exception e) {
				}
			}
		}
		
		return rt;

	}
	
	
	
	
	public Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}




	public void setPassword(String password) {
		this.password = password;
	}




	public void setUrl(String url) {
		this.url = url;
	}




	public void setUsername(String username) {
		this.username = username;
	}
}

⌨️ 快捷键说明

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