Java_连接MySQL数据库(三种方法-JDBC)

3 篇文章 0 订阅
2 篇文章 0 订阅

Java_连接MySQL数据库(三种方法)

第一种方法 : 普通方法 ——例如查询数据库

/**
 * 连接数据库 方法1
 * 
 * @author 道鸢
 *
 */
public class Lianjie {
	public static void main(String[] args) {

		/**
		 * DriverManager :依据数据库的不同,管理JDBC驱动 
		 * Connection :负责连接数据库并担任传送数据的任务
		 *  Statement :由Connection 产生、负责执行SQL语句
		 *   ResultSet:负责保存Statement执行后所产生的查询结果 
		 *   加载驱动 
		 *   建立连接
		 *   发送sql语句
		 *   处理返回结果
		 *   释放资源
		 */
		Connection conn = null; // 负责连接数据库并担任传送数据的任务
		Statement stmt = null; // 由 Connection 产生、负责执行SQL语句
		ResultSet rs = null; // 负责保存Statement执行后所产生的查询结果

		try {
			Class.forName("com.mysql.jdbc.Driver"); // 加载光驱
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			// 建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/epet?useSSL=false", "root", "1234");
			System.out.println("Connection" + conn);
			// 创建statement对象
			stmt = conn.createStatement();
			String zhu = "dog";
			String sql = "select * from "+zhu;    //易注入
			//  插入SQL语句           保存Statement执行后所产生的查询结果
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				System.out.println(id + name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

第二种方法:普通连接 转为 方法使用

第一步:创建连接方法

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

public class common { // 连接方法

	public Connection conn() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/epet?useSSL=false", "root", "1234");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

第二步:创建 关闭流方法

public static void closeAll(ResultSet rs, Statement stmt, Connection conn) {

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

第三步:调用方法

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Tste_cha {

	public static void main(String[] args) {
		common co = new common();
		co.conn();
		Statement state = null;
		ResultSet rs = null;
		Connection con = co.conn();
		
		String sql = "select * from dog";
		
		try {
			state = con.createStatement();
			rs = state.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				System.out.println(id + name);
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			common.closeAll(rs, state, con);
		}
	}
}

第三种方法: 使用配置文件连接数据库

第一步:配置文件 ***.properties 文件类型

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/epet?useSSL=false
user=root
password=1234

第二步: 在功能类中

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
 * @author 道鸢
 * 使用配置文件进行连接数据库
 *
 */

public class TestProperties {

	static Properties pros = null;
	static {
		pros = new Properties();
		try { //加载配置文件
			pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Connection getMusqlConn() {
		try {
			Class.forName(pros.getProperty("mysqlDriver"));
			
				return DriverManager.getConnection(pros.getProperty("url"),pros.getProperty("user"),pros.getProperty("password"));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	public static void close(Statement ps, Connection conn,ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值