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

Posted zerobased

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了纯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();
		}
}

 

} }

 

以上是关于纯java方式连接数据库简单操作的主要内容,如果未能解决你的问题,请参考以下文章

怎么使用JAVA连接数据库?

JAVA中如何通过JDBC-ODBC桥来连接SQLServer数据库

DataX通过纯Java代码启动

Airflow 中文文档:保护连接

java数据库访问—JdbcTemplate方式(模板设计模式)

第三方框架-纯代码布局:Masonry的简单使用