JDBC详解及总结

首先,获得数据库的连接,获得一个可配置的连接,是应用程序与数据库的一个“握手”过程。

public class Test{
	public static void main(String[] args){
		Test test = new Test();
		test.list();
	}
	public void list(){
		DBUtil util = new DBUtil();
		Connection conn = util.getConnection();
		String sql = "select id, username, password from UserTbl";
		try{
			Statement stmt =conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String username = rs.getString(2);
				String password = rs.getString(3);
				System.out.println(id+":"+username+":"+password);
			}
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			util.closeConnection(conn);
		}
	}
}
获得数据库连接的步骤:

Class.forName加载驱动

--Class.forName("com.mysql.jdbc.Driver");

DriverManager获得连接

--DriverManager.getConnection("jdbc:mysql://localhost:3306/xxxx_db","root","password");

public class DBUtil{
	public static void main(String[] args){
		DBUtil util = new DBUtil();
		//Connection conn = util.getConnection();		//前两种办法
		Connection conn = util.openConnection();		//第三种办法
	}
	public Connection getConnection(){
		try{
			//这是第一种最简陋的办法
			//-----------------
			Class.forName("com.mysql.jdbc.Driver");
			return DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","password");
			//-----------------
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public Connection getConnection(String url, String driver, String username, String password){
		try{
			//这是第二种办法
			//-----------------
			Class.forName(driver);
			return DriverManager.getConnection(url, username, password);
			//-----------------
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	//第三种办法,把数据库信息写入配置文件中,注意配置文件要保存在相应的工程包中
	//DBConfig.properties
	//文件中写入:
	//driver = com.mysql.jdbc.Driver
	//url = jdbc:mysql://localhost:3306/jdbc_db
	//username = root
	//password = password
	public Connection openConnecion(){
		Properties prop = new Properties();
		String username = null;
		String url = null;
		String driver = null;
		String password = null;
		try{
			prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
			driver = prop.getProperty("driver");
			url = prop.getProperty("url");
			username = prop.getProperty("username");
			password = prop.getProperty("password");
			Class.forName(driver);
			return DriverManager.getConnection(url, username, password);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}



事务:

commit, rollback

public void closeConnetcion(Connection conn){
	try{
		conn.close();
	}catch(SQLException e)
	{
		e.printStackTrace();
	}
}
public void add(){
	DBUtil util = new DBUtil();
	Connection conn = util.openConnecion();
	String sql = "insert into UserTbl(id, username, password) value (3, 'david', '00000')";
	try
	{
		Statement stmt = conn.createStatement();
		stmt.executeUpdate(sql);
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		util.closeConnetcion(conn);
	}
}
//在main函数中加入语句:
//util.add();
//则成功在数据库中插入数据
//若在add()中加入语句:
public void add(){
	DBUtil util = new DBUtil();
	Connection conn = util.openConnecion();
	String sql = "insert into UserTbl(id, username, password) value (3, 'david', '00000')";
	try
	{
		conn.setAutoCommit(false);//加入这个语句,表示不自动提交
		Statement stmt = conn.createStatement();
		stmt.executeUpdate(sql);
		conn.commit(); //必须加入这句,才会将数据插入库中
	}catch(Exception e){
		e.printStackTrace();
		conn.rollback();//若抛出异常,则回滚,即上述try语句块无效
	}finally{
		util.closeConnetcion(conn);
	}
}


你可能感兴趣的:(JDBC详解及总结)