纯java方式连接数据库简单操作

//JDBC全称java datebese connection --java数据库连接

package com.beiwo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

/*数据库帮助类*/
public class DBUtils {
   private static String driverClass;
   private static String url;
   private static String user;
   private static String password;
	
	static{
		ResourceBundle rb=ResourceBundle.getBundle("jdbc");
		driverClass=rb.getString("driverClass");
		url=rb.getString("url");
		user=rb.getString("user");
		password=rb.getString("password");
		try {
			//加载驱动
			Class.forName(driverClass);
		} catch (Exception e) {
		   e.printStackTrace();
		}
	}
	
	public static Connection getConn() throws SQLException{
		
		return DriverManager.getConnection(url, user, password);
	}
	public static void closeAll(ResultSet rs,Statement stme,Connection conn){
		if (null!=rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (null!=rs) {
			try {
				stme.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (null!=rs) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
package com.beiwo;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

public class MasterCurd {

	
public static void findAll(){
		
		Connection conn=null;
	      Statement stme=null;
	      ResultSet rs=null;
	      String sql="SELECT * FROM master";
		  //加载驱动
		try {
			//Class.forName("com.mysql.jdbc.Driver");
			System.out.println("加载成功");
			conn=DBUtils.getConn();
			stme= conn.createStatement();
			rs= stme.executeQuery(sql);
			while (rs.next()) {
				System.out.println(rs.getObject(1));
				System.out.println(rs.getObject(2));
				System.out.println(rs.getObject(3));
				System.out.println(rs.getObject(4));
				System.out.println("=============================");
			}
		} catch (Exception e) {
			
		}finally{
			
			DBUtils.closeAll(rs, stme, conn);
		}
		
	}
//插入
public static void Insert (){
	
    Connection conn=null;
    Statement stme=null;
	  String sql="INSERT INTO master (name, password, money) VALUES ('张政','123', 100)";
	 
	 
	 try {
		//获得连接对象
		conn= DBUtils.getConn();
		System.out.println("连接成功");
		 //获取Statement对象
		stme=conn.createStatement();
		//执行语句
		stme.execute(sql);
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		
		DBUtils.closeAll(null, stme, conn);
	}
		
}
//更新
public static void Update(){
	
    Connection conn=null;
    Statement stme=null;
    String sql="UPDATE master SET name='张军',money=50 WHERE id=6";
	 
	 
	 try {
		//获得连接对象
		conn= DBUtils.getConn();
		System.out.println("连接成功");
		 //获取Statement对象
		stme=conn.createStatement();
		//执行语句
		stme.executeUpdate(sql);
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		
		DBUtils.closeAll(null, stme, conn);
	}
		
}
//删除
public static void Delete(){
    Connection conn=null;
    Statement stme=null;
    String sql="DELETE FROM master WHERE id=7";
	 
	 
	 try {
		//获得连接对象
		conn= DBUtils.getConn();
		System.out.println("连接成功");
		 //获取Statement对象
		stme=conn.createStatement();
		//执行语句
		stme.executeUpdate(sql);
		
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		
		DBUtils.closeAll(null, stme, conn);
	}
		
}
	
}

 

package com.beiwo;

/*import org.junit.Before;*/
import org.junit.Test;

public class TestMasterCrud {
/*	MasterCurd curd=null;
	@Before
        在前面运行
	public void befor(){   
		curd=new MasterCurd();
		
	}*/
	@Test
   public void testFindAll(){
		MasterCurd.findAll();
	}
	@Test
	   public void Insert(){
			MasterCurd.Insert();
		}
	@Test
	   public void Update(){
			MasterCurd.Update();
		}
	@Test
	   public void Delete(){
			MasterCurd.Delete();
		}
}

 

} }

 

posted @ 2016-12-12 19:57  zerobased  阅读(957)  评论(0编辑  收藏  举报