Java自学-MySQL(8)
1、数据库的三大范式
2、数据库驱动和JDBC
3、第一个JDBC程序
package lesson01;
/**
* Author: Gu Jiakai
* Date: 2021/8/27 21:18
* FileName: JdbcDemo01
* Description:
*/
import java.sql.*;
public class JdbcDemo01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver"); //固定写法
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&&useSSL=false";
String name = "root";
String password = "123456";
//3.连接成功,返回数据库对象,connection代表数据库
Connection connection = DriverManager.getConnection(url, name, password);
//4.执行SQL的对象statement
Statement statement = connection.createStatement();
//5.执行SQL的对象,去执行SQL 可能存在结果,查看返回结果
String sql = "SELECT * FROM users";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
System.out.println("id=" + resultSet.getObject("id"));
System.out.println("name=" + resultSet.getObject("NAME"));
System.out.println("pwd=" + resultSet.getObject("PASSWORD"));
System.out.println("email=" + resultSet.getObject("email"));
System.out.println("birthday=" + resultSet.getObject("birthday"));
System.out.println("--------");
}
//6.释放连接
resultSet.close();
statement.close();
connection.close();
}
}
4、JDBC中对象解释
5、Statement对象详解
JdbcUtils.java
package lesson02.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* Author: Gu Jiakai
* Date: 2021/8/28 15:51
* FileName: JdbcUtils
* Description:
*/
public class JdbcUtils {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String password = null;
static {
try {
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(in);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//1、驱动只用加载一次
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
//释放连接资源
public static void release(Connection conn, Statement st, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
TestInsert.java
package lesson02;
import lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Author: Gu Jiakai
* Date: 2021/8/28 16:32
* FileName: TestInsert
* Description:
*/
public class TestInsert {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection();//获取数据库连接
st=conn.createStatement();//获得SQL的执行对象
String sql="insert \n" +
"into users\n" +
"values(4,‘caiwei‘,‘123456‘,‘caiwei@qq.com‘,‘2001-01-01‘)";
int i=st.executeUpdate(sql);
if(i>0){
System.out.println("插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
TestDelete.java
package lesson02;
import lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Author: Gu Jiakai
* Date: 2021/8/28 16:47
* FileName: TestDelete
* Description:
*/
public class TestDelete {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection();//获取数据库连接
st=conn.createStatement();//获得SQL的执行对象
String sql="delete\n" +
"from users\n" +
"where id=4";
int i=st.executeUpdate(sql);
if(i>0){
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
TestUpdate.java
package lesson02;
import lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Author: Gu Jiakai
* Date: 2021/8/28 16:50
* FileName: TestUpdate
* Description:
*/
public class TestUpdate {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection();//获取数据库连接
st=conn.createStatement();//获得SQL的执行对象
String sql="update users\n" +
"set `NAME`=‘caiwei‘,email=‘caiwei@qq.com‘\n" +
"where id=1";
int i=st.executeUpdate(sql);
if(i>0){
System.out.println("更新成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
TestSelect.java
package lesson02;
import lesson02.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Author: Gu Jiakai
* Date: 2021/8/28 16:56
* FileName: TestSelect
* Description:
*/
public class TestSelect {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn = JdbcUtils.getConnection();
st=conn.createStatement();
String sql="select * from users where id=1";
rs = st.executeQuery(sql);//查询完毕会返回一个结果集
while(rs.next()){
System.out.println(rs.getString("NAME"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
}
}
}