1. 程式人生 > 實用技巧 >通過jdbc連線mysql

通過jdbc連線mysql

通過jdbc連線mysql

1.新增maven依賴

倉庫地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java
maven依賴:
	<dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.47</version>
	</dependency>

2.資料庫檔案:

2.1.建立資料庫:
	CREATE DATABASE IF NOT EXISTS test_db DEFAULT CHARACTER SET utf8mb4;
2.2.建立表:
		CREATE TABLE IF NOT EXISTS `class`(
		    `id` int(10) not null AUTO_INCREMENT comment '主鍵',
		    `name` varchar(50) not null default '' comment '名稱',
		    `del_flag` tinyint(4) not null default '0' comment '刪除標識 0為未刪除 1為已刪除',
		    PRIMARY KEY(`id`)
	    )ENGINE=InnoDB default charset=utf8 comment='班級';
2.3.新建資料: 
		INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級a', 0);
		INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級b', 0);
		INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級c', 0);
		INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級d', 0);
		INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級e', 0);
		INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級f', 0);

3.程式碼:

/**
 * 初始化資料庫連線
 */
public static Connection init() throws SQLException, ClassNotFoundException {
	Class.forName("com.mysql.jdbc.Driver") ;
	String url = "jdbc:mysql://127.0.0.1:3306/test_db?useSSL=false&useUnicode=true&amp;amp;characterEncoding=utf8&amp;amp;autoReconnect=true" ;
	String username = "root" ;
	String password = "12345678" ;
	return DriverManager.getConnection(url , username , password ) ;
}	
/**
 * 查詢
 */
public static void select() {
	try {
		Connection con = init();
		String sql = "select * from class";
		PreparedStatement pstmt = con.prepareStatement(sql) ;
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()) {
			System.out.println(rs.getInt(1) + "----" + rs.getString(2) + "----" + rs.getInt(3));
		}
		closeObject(con,pstmt,rs);
	} catch (Exception e) {
		e.printStackTrace();
	}
}	
/**
 * 關閉資料庫物件
 */
public static void closeObject(Connection con,PreparedStatement pstmt,ResultSet rs) {
	if(pstmt != null){   // 關閉宣告
		try{
			pstmt.close() ;
		}catch(SQLException e){
			e.printStackTrace() ;
		}
	}
	if(con != null){  // 關閉連線物件
		try{
			con.close() ;
		}catch(SQLException e){
			e.printStackTrace() ;
		}
	}
	if (rs != null) {
		try{
			rs.close() ;
		}catch(SQLException e){
			e.printStackTrace() ;
		}
	}
}	
/**
 * 新增
 */
public static void insert() {
	try {
		Connection con = init();
		String sql = "INSERT INTO `class`( `name`, `del_flag`) VALUES ('班級h', 0);";
		PreparedStatement pstmt = con.prepareStatement(sql) ;
		int count = pstmt.executeUpdate();
		if (count >= 1) {
			System.out.println("新增成功");
			select();
		}
		closeObject(con,pstmt,null);
	} catch (Exception e) {
		e.printStackTrace();
	}
}	
/**
 * 更新
 */
public static void update() {
	try {
		Connection con = init();
		String sql = "UPDATE class set name = '修改班級名字' where id = 1;";
		PreparedStatement pstmt = con.prepareStatement(sql) ;
		int count = pstmt.executeUpdate();
		if (count >= 1) {
			System.out.println("修改成功");
			select();
		}
		closeObject(con,pstmt,null);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
/**
 * 刪除
 */
public static void delete() {
	try {
		Connection con = init();
		String sql = "delete from class where id = 1;";
		PreparedStatement pstmt = con.prepareStatement(sql) ;
		int count = pstmt.executeUpdate();
		if (count >= 1) {
			System.out.println("刪除成功");
			select();
		}
		closeObject(con,pstmt,null);
	} catch (Exception e) {
		e.printStackTrace();
	}
}