Java连接数据库及所需jar包

news/2024/5/18 21:49:26 标签: 数据库, mysql, jdbc

数据库连接代码:

package com.classify.dao;

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

import com.mysql.cj.xdevapi.PreparableStatement;

public class Basedao {
	static {
		//加载驱动
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public static Connection getconn() {
		//创建一个链接对象
		Connection conn = null;
		
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/你要连接的数据库名字?serverTimezone=UTC","用户名(一般是root)","登录数据库密码(一般是123456)");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	
	public static int exectuIUD(String sql,Object[] params) {
		
		int count = 0;
		Connection conn = Basedao.getconn();
		//准备sql语句
		PreparedStatement ps = null;
		
		//insert into user("","","")value(?,?,?)
		try {
			//准备好语句
			ps = conn.prepareStatement(sql);
			
			for( int i=0;i<params.length;i++) {
				ps.setObject(i+1, params[i]);
			}
			//执行语句
			count = ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			Basedao.closeall(null, ps, conn);
		}
		
		return count;
	}
	
	public static void closeall(ResultSet rs, PreparedStatement ps, Connection conn) {
		
		try {
			if(rs!=null) {
				rs.close();
			}
			if(ps!=null)
				ps.close();
			if(conn!=null)
				conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}

http://www.niftyadmin.cn/n/1269213.html

相关文章

STL容器deque

Deque 定义于头文件 <deque> template<class T, class Allocator std::allocator<T>> class deque; Deque的能力 Deque与vector相比&#xff0c;功能上的差异如下&#xff1a; 1. 两端都可以快速插入和删除元素&#xff0c;而vector只可以后端。 2. …

STL容器list

list 定义于头文件 <list> template<class T, class Allocator std::allocator<T>> class list; 列表是一个容器&#xff0c;它支持任何位置的元素快速插入和删除&#xff0c;不支持快速的随机访问。它被实现为双向的链表。与std::forward_list相比&…

使用poi创建表,HSSFWorkbook,HSSFSheet,HSSFRow

实现代码&#xff1a; package com.classify.servlet;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; impor…

STL容器forward_list

Forward list 定义于头文件 <forward_list> template<class T, class Allocator std::allocator<T>> class forward_list; (C11 起) 单向列表是一个容器&#xff0c;支持在其任何地方快速插入和删除元素&#xff0c;不支持快速的随机访问。它被实现为单…

tf.keras.layers.Flatten

tf.keras.layers.Flatten 使输入展平&#xff0c;不会影响批处理的大小。如果输入形状为(batch,)&#xff0c;没有特征轴&#xff0c;则展平会增加额外的通道尺寸&#xff0c;输出形状为(batch&#xff0c;1)。 tf.keras.layers.Flatten(data_formatNone, **kwargs )data_for…

STL容器Set和Multiset

Set和Multiset 定义于头文件 <set> template<class Key,class Compare std::less<Key>, class Allocator std::allocator<Key>> class set; std::set是一个关联容器&#xff0c;是一个有序的集合&#xff0c;集合中包含不可重复的、类型为Key的元…

STL容器Map和Mutimap

定义于头文件 <map> template<class Key,class T,class Compare std::less<Key>, class Allocator std::allocator<std::pair<const Key, T> >> class map; std::map是一个有序关联容器&#xff0c;包含具有唯一键的键值对。键使用比较函数…