Day7.7.rar
javaweb.zip
Jdbc增删改查
package com.xx.Jdbc.demo;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.Scanner;
import org.junit.Test;
import java.sql.Connection;
public class Demo {
/**
* 查询
*/
@Test
public void Demo1() throws SQLException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JunitDemo.getConnection();
stmt = conn.createStatement();
String sql = "select * from t_user";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("id:"+rs.getInt("uid")+"name:"+rs.getString("name")+"password:"+rs.getString("password"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(rs ,stmt, conn);
}
}
/**
* 更新
*/
@Test
public void Demo2() throws SQLException{
Connection conn = null;
Statement stmt = null;
try {
conn = JunitDemo.getConnection();
stmt = conn.createStatement();
String sql = "update t_user set password='xxxxxx' where uid=4";
int num = stmt.executeUpdate(sql);
if (num>0) {
System.out.println("成功");
}else {
System.out.println("失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(stmt, conn);
}
}
/**
* 添加
*/
@Test
public void Demo3() throws SQLException{
Connection conn = null;
Statement stmt = null;
try {
conn = JunitDemo.getConnection();
stmt = conn.createStatement();
String sql = "insert into t_user (name,password) values ('柒萧萧','qs12345')";
int num = stmt.executeUpdate(sql);
if (num>0) {
System.out.println("成功");
}else {
System.out.println("失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(stmt, conn);
}
}
/**
* 删除
*/
@Test
public void Demo4() throws SQLException{
Connection conn = null;
Statement stmt = null;
try {
conn = JunitDemo.getConnection();
stmt = conn.createStatement();
String sql = "delete from t_user where uid = 4";
int num = stmt.executeUpdate(sql);
if (num>0) {
System.out.println("成功");
}else {
System.out.println("失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(stmt, conn);
}
}
/**
* 登录
*/
@Test
public void Demo5() throws SQLException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JunitDemo.getConnection();
stmt = conn.createStatement();
Scanner scanner = new Scanner(System.in);
System.out.println("用户名");
String user = scanner.next();
System.out.println("密码");
String password = scanner.next();
String sql = "select count(*) from t_user where name='"+user+"' and password='"+password+"'";
rs = stmt.executeQuery(sql);
while (rs.next()) {
if (rs.getInt(1)>0) {
System.out.println("成功");
}else {
System.out.println("失败");
System.exit(0);
return;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(stmt, conn);
}
}
}
PreparedStatement之增删改查
/**
* PreparedStatement登录
*/
@Test
public void Demo6() throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JunitDemo.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.print("用户名");
String user = scanner.next();
System.out.print("密码");
String password = scanner.next();
String sql = "select count(*) from t_user where name=? and password=?";
// System.out.println(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
while (rs.next()) {
if (rs.getInt(1)>0) {
System.out.println("成功");
}else {
System.out.println("失败");
System.exit(0);
return;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(rs, pstmt, conn);
}
}
/**
* PreparedStatement插入
*/
@Test
public void Demo7() throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JunitDemo.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("用户名");
String user = scanner.next();
System.out.println("密码");
String password = scanner.next();
String sql = "insert into t_user (name,password) values (?,?)";
System.out.println(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user);
pstmt.setString(2, password);
int num = pstmt.executeUpdate();
if (num>0) {
System.out.println("成功");
}else {
System.out.println("失败");
System.exit(0);
return;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(pstmt, conn);
}
}
/**
* PreparedStatement删除
*/
@Test
public void Demo8() throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JunitDemo.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("删除ID");
int id = scanner.nextInt();
String sql = "delete from t_user where id=?";
// System.out.println(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int num = pstmt.executeUpdate();
if (num>0) {
System.out.println("成功");
}else {
System.out.println("失败");
System.exit(0);
return;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(pstmt, conn);
}
}
/**
* PreparedStatement更新
*/
@Test
public void Demo9() throws SQLException{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JunitDemo.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.println("更新ID");
int id = scanner.nextInt();
System.out.println("用户名修改为");
String up_name = scanner.next();
String sql = "update t_user set name=? where id=?";
// System.out.println(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, up_name);
pstmt.setInt(2, id);
int num = pstmt.executeUpdate();
if (num>0) {
System.out.println("成功");
}else {
System.out.println("失败");
System.exit(0);
return;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JunitDemo.release(pstmt, conn);
}
}
JdbcUtils工具类
package com.xx.JdbcUtils.Demo;
import java.sql.*;
public class JdbcUtils {
/**
* 声明成员变量
* */
private static final String driverClassName;
private static final String url;
private static final String user;
private static final String password;
/**
* 成员变量赋值
* */
static {
driverClassName = "com.mysql.jdbc.Driver"; //mysql jar包
url = "jdbc:mysql://localhost:3306/stusys";//数据库链接
user = "root"; //数据库用户名
password = "root"; //数据库密码
}
/**
* 加载驱动
* */
public static void loadDriver() throws ClassNotFoundException {
Class.forName(driverClassName);
}
/**
* 获得链接
* */
public static Connection getConnection() throws ClassNotFoundException {
loadDriver();//加载驱动
Connection conn = null;
try {
conn = DriverManager.getConnection(url,user,password); //获得链接
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 释放资源 Statement Connection
**/
public static void release(Statement stmt,Connection conn) throws SQLException {
stmt.close();
conn.close();
}
/**
* 释放资源 ResultSet Statement Connection
* */
public static void release(ResultSet rs, Statement stmt, Connection conn) throws SQLException {
rs.close();
stmt.close();
conn.close();
}
}
使用JdbcUtils工具类
package day;
import com.xx.JdbcUtils.Demo.JdbcUtils;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Demo {
//声明全局遍变量
Connection conn;
PreparedStatement pstmt;
ResultSet rs;
/**
* 查询
* */
@Test
public void demo() throws ClassNotFoundException, SQLException {
String sql = "select * from sort";//编写sql语句
conn = JdbcUtils.getConnection(); //获取链接 调取JdbcUtils工具类
pstmt = conn.prepareStatement(sql);//执行sql语句
rs = pstmt.executeQuery();
//遍历集合
while (rs.next()){
//输出集合内容 ID user
System.out.println(rs.getInt("id")+"++++"+rs.getString("user") );
}
}
}
db.properties
Properties properties = new Properties();
properties.load(new FileInputStream("src/db.properties"));
driverClassName = properties.getProperty("driverClassName");
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/rubysiu
name=root
password=root
QueryRunner
/**
* 获取多条记录
* @throws SQLException
* */
@Test
public void getAll() throws SQLException {
QueryRunner qr = new QueryRunner();
String sql = "selcet * from Student";
List<Student> list = qr.query(JunitDemo.getConnection(),sql,new BeanListHandler<Student>(Student.class));
for (Student student : list) {
System.out.println(student.toString());
}
}
String sql = "selcet * from Student where name=?";
/**
* 获取一条记录
* @throws SQLException
* */
@Test
public void getOne() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.println("用户名");
String name = scanner.next();
QueryRunner qr = new QueryRunner();
String sql = "selcet * from Student where name=?";
Object[] params={name};
Student student = qr.query(JunitDemo.getConnection(), sql,new BeanHandler<Student>(Student.class),params);
System.out.println(student.toString());
}