T9
作者:互联网
package T9;
import java.sql.Connection; // 连接对象
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBConnect {
// 1. 配置信息
// 配置驱动位置
private static final String DRIVER = "com.mysql.jdbc.Driver";
// private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
// 配置数据库URL
private static final String URL = "jdbc:mysql://127.0.0.1:3306/soft1";
// 配置账号
private static final String USER = "root";
// 配置密码
private static final String PWD = "toor"; // 1234
// 2. 在静态代码块中注册驱动
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 3. 创建连接对象 写到方法里面
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PWD);
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 插入
public static boolean indsertLine() {
// 1. 获取连接对象
Connection conn = getConnection();
// 2. 编写sql
String sql = "insert into line values(null,?,?,?,?)";
// 3. 执行sql
// 3.1 创建PreparedStatement 对象 预编译sql
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 3.2 给sql里面的?赋值
pstmt.setString(1, "601");
pstmt.setString(2, "六一公司");
pstmt.setDouble(3, 6.1);
pstmt.setInt(4, 61);
// 3.3 执行 executeUpdate
int result = pstmt.executeUpdate(); // 返回受影响的行数 整数
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 删除
public static boolean deleteLine() {
// 1. 获取连接对象
Connection conn = getConnection();
// 2. 编写sql
String sql = "delete from line where lineID=?";
// 3. 执行sql
// 3.1 创建PreparedStatement 对象 预编译sql
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 3.2 给sql里面的?赋值
pstmt.setInt(1,1);
// 3.3 执行 executeUpdate
int result = pstmt.executeUpdate(); // 返回受影响的行数 整数
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 修改
public static boolean updateLine() {
// 1. 获取连接对象
Connection conn = getConnection();
// 2. 编写sql
String sql = "update line set lineNo=?, company=?, number=? where lineID=1";
// 3. 执行sql
// 3.1 创建PreparedStatement 对象 预编译sql
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 3.2 给sql里面的?赋值
pstmt.setString(1, "6111");
pstmt.setString(2, "公交六一公司");
pstmt.setInt(3, 610);
// 3.3 执行 executeUpdate
int result = pstmt.executeUpdate(); // 返回受影响的行数 整数
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 临时测试
public static void main(String[] args) {
Connection conn = DBConnect.getConnection();
System.out.println(conn);
// 测试插入
// boolean res = DBConnect.indsertLine();
// System.out.println("插入:" + res);
// 测试修改
// boolean res_update = DBConnect.updateLine();
// System.out.println("插入:" + res_update);
// 测试删除
boolean res_delete = DBConnect.deleteLine();
System.out.println("删除:"+res_delete);
}
}
import java.sql.Connection; // 连接对象
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBConnect {
// 1. 配置信息
// 配置驱动位置
private static final String DRIVER = "com.mysql.jdbc.Driver";
// private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
// 配置数据库URL
private static final String URL = "jdbc:mysql://127.0.0.1:3306/soft1";
// 配置账号
private static final String USER = "root";
// 配置密码
private static final String PWD = "toor"; // 1234
// 2. 在静态代码块中注册驱动
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// 3. 创建连接对象 写到方法里面
public static Connection getConnection() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, USER, PWD);
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 插入
public static boolean indsertLine() {
// 1. 获取连接对象
Connection conn = getConnection();
// 2. 编写sql
String sql = "insert into line values(null,?,?,?,?)";
// 3. 执行sql
// 3.1 创建PreparedStatement 对象 预编译sql
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 3.2 给sql里面的?赋值
pstmt.setString(1, "601");
pstmt.setString(2, "六一公司");
pstmt.setDouble(3, 6.1);
pstmt.setInt(4, 61);
// 3.3 执行 executeUpdate
int result = pstmt.executeUpdate(); // 返回受影响的行数 整数
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 删除
public static boolean deleteLine() {
// 1. 获取连接对象
Connection conn = getConnection();
// 2. 编写sql
String sql = "delete from line where lineID=?";
// 3. 执行sql
// 3.1 创建PreparedStatement 对象 预编译sql
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 3.2 给sql里面的?赋值
pstmt.setInt(1,1);
// 3.3 执行 executeUpdate
int result = pstmt.executeUpdate(); // 返回受影响的行数 整数
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 修改
public static boolean updateLine() {
// 1. 获取连接对象
Connection conn = getConnection();
// 2. 编写sql
String sql = "update line set lineNo=?, company=?, number=? where lineID=1";
// 3. 执行sql
// 3.1 创建PreparedStatement 对象 预编译sql
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 3.2 给sql里面的?赋值
pstmt.setString(1, "6111");
pstmt.setString(2, "公交六一公司");
pstmt.setInt(3, 610);
// 3.3 执行 executeUpdate
int result = pstmt.executeUpdate(); // 返回受影响的行数 整数
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
// 临时测试
public static void main(String[] args) {
Connection conn = DBConnect.getConnection();
System.out.println(conn);
// 测试插入
// boolean res = DBConnect.indsertLine();
// System.out.println("插入:" + res);
// 测试修改
// boolean res_update = DBConnect.updateLine();
// System.out.println("插入:" + res_update);
// 测试删除
boolean res_delete = DBConnect.deleteLine();
System.out.println("删除:"+res_delete);
}
}
标签:String,T9,SQLException,static,sql,conn,pstmt 来源: https://www.cnblogs.com/Lappland520/p/16348502.html