• 近期整理的 JDBCTools


    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import com.mysql.jdbc.PreparedStatement;
    
    /**
     * JDBC 的工具类
     * 
     * 其中包含: 获取数据库连接, 关闭数据库资源等方法.
     */
    public class JDBCTools {
    
    	// 处理数据库事务
    	// 提交事务
    	public static Connection getConn(String ServiceDb) throws IOException {
    		String driver = "com.mysql.jdbc.Driver";
    		String db = null;
    		String username = null;
    		String password = null;
    		Connection conn = null;
    		
    		ClassLoaderTest properties = new ClassLoaderTest();
    		db = properties.getProperties("db.properties", "db_test_env") + ServiceDb;
    		username = properties.getProperties("db.properties", "username_test_env");
    		password = properties.getProperties("db.properties", "password_test_env");
    		//System.out.println("@@@@@@ db = " + db +"@@@@@@ username = " + username +"@@@@@@ password = " + password );
    		
    		try {
    			Class.forName(driver); // classLoader,加载对应驱动
    			conn = (Connection) DriverManager.getConnection(db, username, password);
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return conn;
    	}
    
    	public static int insert(String sql,String ServiceDb, String table, String column) throws IOException {
    		Connection conn = getConn(ServiceDb);
    		int i = 0;
    		PreparedStatement pstmt;
    		try {
    			pstmt = (PreparedStatement) conn.prepareStatement(sql);
    			pstmt.setString(1, table + "." + column + "()");
    			i = pstmt.executeUpdate();
    			pstmt.close();
    			conn.close();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return i;
    	}
    
    	public static int update(String sql, String ServiceDb) throws IOException {
    		Connection conn = getConn(ServiceDb);
    		int i = 0;
    		PreparedStatement pstmt;
    		try {
    			pstmt = (PreparedStatement) conn.prepareStatement(sql);
    			i = pstmt.executeUpdate();
    			System.out.println("resutl: " + i);
    			pstmt.close();
    			conn.close();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return i;
    	}
    
    	public static String getAll(String sql, String ServiceDb) throws IOException {
    		Connection conn = getConn(ServiceDb);
    		PreparedStatement pstmt;
    		try {
    			pstmt = (PreparedStatement) conn.prepareStatement(sql);
    			ResultSet rs = pstmt.executeQuery();
    			int col = rs.getMetaData().getColumnCount();
    			System.out.println("============================");
    			while (rs.next()) {
    				for (int i = 1; i <= col; i++) {
    					System.out.print(rs.getString(i) + "	");
    					if ((i == 2) && (rs.getString(i).length() < 8)) {
    						System.out.print("	");
    					}
    				}
    				System.out.println("");
    			}
    			System.out.println("============================");
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static String selectResult(String sql, String ServiceDb) throws SQLException, IOException {
    		String str = null;
    		Connection conn = getConn(ServiceDb);
    		PreparedStatement pstmt;
    		pstmt = (PreparedStatement) conn.prepareStatement(sql);
    		ResultSet rs = pstmt.executeQuery();
    		while(rs.next())
            {
                int col=1;
                str=rs.getString(col);
            }
    
    		return str;
    	}
    
    	public static int delete(String sql, String ServiceDb) throws IOException {
    		Connection conn = getConn(ServiceDb);
    		int i = 0;
    		PreparedStatement pstmt;
    		try {
    			pstmt = (PreparedStatement) conn.prepareStatement(sql);
    			i = pstmt.executeUpdate();
    			System.out.println("resutl: " + i);
    			pstmt.close();
    			conn.close();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return i;
    	}
    	
    	public static ResultSet selectResultSet(String sql, String ServiceDb) throws SQLException, IOException {
    		Connection conn = getConn(ServiceDb);
    		PreparedStatement pstmt;
    		pstmt = (PreparedStatement) conn.prepareStatement(sql);
    		ResultSet rs = pstmt.executeQuery();
    		return rs;
    	}
    			
    	public static String selectResultRandom(String sql, String ServiceDb) throws SQLException, IOException {
    		String str = null;
    		Connection conn = getConn(ServiceDb);
    		PreparedStatement pstmt;
    		pstmt = (PreparedStatement) conn.prepareStatement(sql);
    		ResultSet rs = pstmt.executeQuery();
    		List<String> list=new ArrayList<String>();		
    		while(rs.next())
            {
                int col=1;
                str=rs.getString(col);
                list.add(str);                               
            }
    		 Random rnd=new Random();
             int b = rnd.nextInt(list.size());
             str = list.get(b);
    		return str;
    	}
    	
    	
    }
    

      

  • 相关阅读:
    文件过滤驱动隐藏目标文件
    POJ 3345 Bribing FIPA(树形DP)
    POJ 1018 Communication System(分组背包DP)
    无用的,ring0暴力枚举进程模块
    HDOJ 3496 Watch The Movie(基本二维背包)
    栈回溯法的一个例子
    代码这样写奇丑无比...编码前期要做好规划工作啊
    多核发dpc安全inline hook
    纵我不往,知识不来学习Java第一周心得
    对“TD信息树”的使用体验
  • 原文地址:https://www.cnblogs.com/by170628/p/8134537.html
Copyright © 2020-2023  润新知