⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbinfo.java

📁 Java代码实现的数据库链接池
💻 JAVA
字号:
package crawer.store.db;

import java.sql.*;

public class DBInfo {
	
	private static String dburl;
	
	private static String mySqlDriver = "org.gjt.mm.mysql.Driver";
	
	private static String username;
	
	private static String passwd;
	
	
	static{
		initDB();
	}
	
	/**
	 * do some db initial work here
	 * read urls, dbuser, passwd
	 * 
	 */
	private static void initDB(){
		mySqlDriver = "org.gjt.mm.mysql.Driver";
		dburl = "jdbc:mysql://localhost/sina?autoReconnect=true&useUnicode=true&characterEncoding=gbk";
		username = "root";
		passwd = "root";
		try {
			Class.forName(mySqlDriver);
		} catch (ClassNotFoundException e) {		
			e.printStackTrace();
		}
	}
	
	/**
	 * 这个方法是给连接池用的,其他package是看不到的。。
	 * @return
	 */
	static Connection getConnection() throws SQLException{
		Connection conn = null;		
		conn = DriverManager.getConnection( dburl,username,passwd);		
		return conn;
	}
	
	
	//charset test. ok 
	//utf -8
	
	private static void viewDb(){
		Connection conn = null;
		Statement stmt = null;
		try{
			conn = getConnection();
			stmt = conn.createStatement();
			String sql = "select title, author from page_id";
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				String title = rs.getString("title");
				String author = rs.getString("author");
				System.out.print(title+"::"+author+"\n");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private static void insert(){
		Connection conn = null;
		Statement stmt = null;
		 try {			
			conn = DriverManager.getConnection( dburl,username,passwd);
			stmt = conn.createStatement();
			String title = "南京夫子庙2";
			String author = "jacky";
			String sql = "insert into page_id (title,author) values ('"+title+"','"+author+"') ";
			stmt.executeUpdate(sql);
			}
		catch (SQLException e) {			
			e.printStackTrace();
		}
	}
	
	
	public static void main(String args[]){
		initDB();
		viewDb();
		 
	}
}

⌨️ 快捷键说明

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