jdbc訪問數據庫

package com.china.bill.java.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DbConnection {
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
	// 連接數據庫
	private void open() 
	{
		try 
		{
			// 加載驅動
			Class.forName("com.mysql.jdbc.Driver");
			// 獲得連接
			conn = DriverManager.getConnection("jdbc:" +
					"mysql://localhost:3306/bill?useUnicode=true&characterEncoding=UTF-8","root","root");
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	// 執行增、刪、改
	public int update(String sql , Object...params)
	{
		int conut = 0;
		
		try 
		{
			// 打開數據庫連接
			open();
			// 執行sql語句,獲取結果
			ps = conn.prepareStatement(sql);
			//給佔位符賦值
			for (int i = 0 ; i < params.length ; i++) 
			{
				ps.setObject(i+1, params[i]);
			}
			// 執行
			conut = ps.executeUpdate();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			close();
		}
		
		return conut;
	}
	
	/**
	 * 執行查詢
	 */
	public ResultSet query(String sql,Object...params) 
	{
		try {
			// 打開連接
			open();
			// 執行sql語句,獲得結果集
			ps = conn.prepareStatement(sql);
			//給佔位符賦值
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i+1, params[i]);
			}
			// 獲得結果
			rs = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	// 關閉連接
	private void close() {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
package com.china.bill.java.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.china.bill.java.entity.EmailAddress;
import com.china.bill.java.util.PortalUtil;

public class AddEmail {
	
	private static final Logger log = LoggerFactory.getLogger(AddEmail.class);
	private static final DbConnection dbConnection = new DbConnection();
	
	public static void main(String[] args) 
	{
		/*EmailAddress emailAddress = new EmailAddress("", "");
		int count = addEmail(emailAddress);
		if (count > 0) 
		{
			System.out.println("新增成功!");
		} 
		else 
		{
			System.out.println("新增失敗!");
		}*/
		EmailAddress emailAddress = findEmilById(2);
		System.out.println("賬號\t" + emailAddress.getEmail() + "\t密碼\t" + emailAddress.getPasswd());
		// 解密
		String passwd = PortalUtil.desPasswd(emailAddress.getPasswd(), emailAddress.getKeyCode());
		System.out.println("密碼\t" + passwd);
	}
	/**
	 * 新增管理員郵箱
	 * @param emailAddress
	 * @return
	 */
	public static int addEmail(EmailAddress emailAddress) 
	{
		Map map = PortalUtil.aesPasswd(emailAddress.getPasswd(), "");
		
		String passwd = null;
		String keyCode = null;
		
		if (map.size() == 2) 
		{
			passwd = map.get("passwd");
			keyCode = map.get("keyCode");
		} 
		else 
		{
			log.debug("加密失敗!");
		}
		
		String sql = "insert into emailaddress (email, passwd, keyCode) values (?, ?, ?)";
		
		
		int count = dbConnection.update(sql, emailAddress.getEmail(), passwd, keyCode);
		return count;
	}
	
	public static EmailAddress findEmilById(Integer id) 
	{
		String sql = "select * from emailaddress where id = ?"; 
		ResultSet rs =  dbConnection.query(sql, id);
		EmailAddress emailAddress = null;
		
		try {
			while (rs.next()) 
			{
				emailAddress = new EmailAddress(rs.getString(2), rs.getString(3), rs.getString(4));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return emailAddress;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章