相同的部分拿出
package com.wenhe.total;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DCji {
private static final String PASSWORD = "root123";
private static final String USERNAME = "root";
private static final String URL = "jdbc:mysql://localhost:3306/movies?useUnicode=true&characterEncoding=utf8";
static{
// 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
/**
* get Connection
* 获取连接
* @return
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭 结果集
*
* @param ResultSet
*/
public static void release(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* Establish Statement
* 关闭 申明
* @param Statement
*/
public static void release(Statement statement) {
if (null != statement) {
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* release Connection
* 关闭连接
* @param Connection
*/
public static void release(Connection connection) {
if (null != connection) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* release ResultSet Statement Connection
*
* @param rs
* @param statement
* @param connection
*/
public static void release(ResultSet rs, Statement statement, Connection connection) {
release(rs);
release(statement);
release(connection);
}
}
增删改
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.wenhe.total.DCji;
public class Delete {
public static void main(String[] args) {
Connection connection = DCji.getConnection();
// 3 创建声明
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
// 4 执行SQL
int count = 0;
try {
count = statement.executeUpdate("DELETE FROM t_employee WHERE id = 19");
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("影响记录行数: " + count);
//关闭申明,关闭连接
DCji.release(null, statement, connection);
}
}
查询
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.wenhe.total.DCji;
public class dada {
public static void main(String[] args) {
Connection connection = DCji.getConnection();
//创建申明
Statement statement = null ;
try {
statement = connection.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//执行sql
ResultSet rs = null ;
try {
rs = statement.executeQuery("SELECT id, name, sex, age, dept_id FROM t_employee");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//遍历结果集
try {
while(rs.next()) {
int id = rs.getInt("id"); //以 Java 编程语言中 int 的形式获取此 ResultSet 对象的当前行中指定列的值。
String name = rs.getString("name");// Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。
System.out.printf("id = %d, name = %s\n", id, name);
}
} catch (SQLException e) {
e.printStackTrace();
}
//关闭结果集,关闭申明,关闭连接
DCji.release(rs, statement, connection);
}
}