版权声明:JAVA https://blog.csdn.net/weixin_43190126/article/details/84846701
package com.qyl.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import org.junit.Test;
public class TestDao {
/**
* 获取链接
* @throws ClassNotFoundException
*/
@Test
public void testGetConn() throws Exception {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
Thread.sleep(5000);
System.out.println(conn);
}
//创建表
/**
* 贾琏欲执事 DDL
*/
@Test
public void testCreatTable() throws Exception {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "123456");
//获取语句对象
Statement st = conn.createStatement();
//sql
String sql = "CREATE TABLE dept(id bigint(20) NOT NULL AUTO_INCREMENT,name varchar(255) DEFAULT NULL,PRIMARY KEY (`id`))";
//执行语句
st.executeUpdate(sql);
//释放资源
st.close();
conn.close();
}
//增添
@Test
public void testCreatTable() throws Exception {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "123456");
//获取语句对象
Statement st = conn.createStatement();
//sql
String sql = "insert into dept(id,name) values (\"1\",'张三')";
//执行语句
st.executeUpdate(sql);
//释放资源
st.close();
conn.close();
}
//修改
@Test
public void testCreatTable() throws Exception {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "123456");
//获取语句对象
Statement st = conn.createStatement();
//sql
String sql = "UPDATE dept set name = \"张五\" WHERE id = 3";
//执行语句
st.executeUpdate(sql);
//释放资源
st.close();
conn.close();
}
//删除
@Test
public void testDelete() throws Exception {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "123456");
st = conn.createStatement();
st.executeUpdate("DELETE from dept WHERE id = 3");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(st!=null)st.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(conn!=null)conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//单个查询
/**
* 贾琏欲执事 DQL
*/
@Test
public void testGetOne() throws Exception {
Dept dept = new Dept();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "123456");
//获取语句对象
Statement st = conn.createStatement();
//sql
String sql = "select * from dept where id = 2";
//执行语句
ResultSet result = st.executeQuery(sql);
while (result.next()) {
dept.setId(result.getInt("id"));
dept.setName(result.getString("name"));
}
//释放资源
st.close();
conn.close();
System.out.println(dept);
}
//多个查询
/**
* 贾琏欲执事 DQL
*/
@Test
public void testGetAll() throws Exception {
ArrayList<Dept> list = new ArrayList<>();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取链接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "123456");
//获取语句对象
Statement st = conn.createStatement();
//sql
String sql = "select * from dept";
//执行语句
ResultSet result = st.executeQuery(sql);
while (result.next()) {
Dept dept = new Dept();
dept.setId(result.getInt("id"));
dept.setName(result.getString("name"));
list.add(dept);
}
//释放资源
result.close();
st.close();
conn.close();
System.out.println(list);
}
}
其中的一些sql语句根据实际情况做修改,还有连接密码与数据库,必要的话也做一些修改;