package dao;

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

public class DbJdbc {
  Connection conn=null;
  Statement st=null;
  ResultSet rs=null;
  
  
  String  driver="com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://localhost:3306/selectcourse?useUnicode=true&characterEncoding=utf8";
  String user="root";
  String pwd="root";
  
  //连接数据库
  public Connection dbConn(){
	  try {
		Class.forName(driver);
		conn=DriverManager.getConnection(url,user,pwd);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  return conn;
  }
  
  
  
	//获取用于向数据库发送SQL语句,并获取结果集
  public void SqlRequest(String sql){
	  try {
		  conn=dbConn();
		st=conn.createStatement();
		rs=st.executeQuery(sql);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }
  
  //查询结果
  public ResultSet selectSql(String sql){
	  try {
		  
		  conn=dbConn();
		st=conn.createStatement();
		rs=st.executeQuery(sql);
	} catch (Exception e) {
		close();
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  return rs;
  }
  
  

  //连接数据库
  public void ConnData(String sql){
	  try {
		Class.forName(driver);
		conn=DriverManager.getConnection(url,user,pwd);
		st=conn.createStatement();
		st.executeUpdate(sql);   //执行增删改查SQL请求语句
		//rs=st.executeQuery(sql); //返回结果集,执行查询功能,进行元素的遍历
	} catch (Exception e) {
		close();
		e.printStackTrace();
	}
	 
  }
  
  
  //关闭对象
  public void close(){
	  try {
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {

			e.printStackTrace();
		}
  }
  
  
//4、增删改
	public void zsg(String sql) {
		dbConn();
		try {
			st = conn.createStatement();
			st.executeUpdate(sql);
		} catch (SQLException e) {
			close();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

  
}
最近下载更多
wuyanmingsi  LV1 2024年10月15日
一勺枣糕  LV3 2024年6月19日
tmf852  LV5 2023年6月10日
Tayirjan  LV11 2023年3月23日
263648  LV7 2022年6月23日
ouyun12345  LV4 2022年5月17日
微信网友_5764743416303616  LV6 2022年4月25日
kuoluoluo  LV3 2022年4月18日
朱朱啊哈  LV16 2022年3月4日
123123123W  LV7 2022年2月28日
最近浏览更多
Maomaoyun 6月10日
暂无贡献等级
2297224153 3月27日
暂无贡献等级
652654393  LV5 2024年12月18日
11onerr 2024年11月22日
暂无贡献等级
111rose  LV1 2024年11月21日
wsq123456 2024年11月20日
暂无贡献等级
wuyanmingsi  LV1 2024年9月7日
GQpqwe 2024年8月14日
暂无贡献等级
琐事消极 2024年7月1日
暂无贡献等级
时光海  LV2 2024年6月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友