package com.study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class TestOne {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql://localhost/jdbcone";
String userName="root";
String passWord="root";
Connection conn = DriverManager.getConnection(url, userName, passWord);
//定义sql
String sql="update users set password=666 where id=1";
//获取执行sql的statement对象
Statement stmt = conn.createStatement();
//执行sql
int count = stmt.executeUpdate(sql); //受影响的行数
//处理结果
System.out.println("受影响的行数:"+count); //受影响的行数:1
//释放资源
stmt.close();
conn.close();
}
}
package com.study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestOne {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql://localhost/jdbcone";
String userName="root";
String passWord="root";
Connection conn = DriverManager.getConnection(url, userName, passWord);
Statement stmt =null;
try {
//开启事务
conn.setAutoCommit(false);
//定义sql
String sql1="update users set password=888 where id=1";
String sql2="update users set password=666 where id=2";
//获取执行sql的statement对象
stmt = conn.createStatement();
//执行sql
int count1 = stmt.executeUpdate(sql1); //受影响的行数
//处理结果
System.out.println("受影响的行数:"+count1); //受影响的行数:1
int count2 = stmt.executeUpdate(sql2); //受影响的行数
//处理结果
System.out.println("受影响的行数:"+count2); //受影响的行数:1
//提交事务
conn.commit();
} catch (Exception e) {
//回滚事务
conn.rollback();
e.printStackTrace();
}
//释放资源
stmt.close();
conn.close();
}
}
/*
受影响的行数:1
受影响的行数:1
*/
package com.study;
import java.sql.*;
public class ResultSetTest {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql://localhost/jdbcone";
String userName="root";
String passWord="root";
Connection conn = DriverManager.getConnection(url, userName, passWord);
Statement stmt = null;
ResultSet rs = null;
try {
//定义sql
String sql="select * from users";
//创建statement对象
stmt = conn.createStatement();
//执行sql语句
rs = stmt.executeQuery(sql);
//遍历rs中的数据
while (rs.next()){
int id = rs.getInt("id");
String name = rs.getString("username");
String password = rs.getString("password");
String email= rs.getString("email");
System.out.println(id);
System.out.println(name);
System.out.println(password);
System.out.println(email);
System.out.println("----------------------------------------");
}
} catch (Exception e) {
e.printStackTrace();
}
//释放资源
rs.close();
stmt.close();
conn.close();
}
}
案例2:
package com.study;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ResultSetTestPlus {
public static void main(String[] args) throws Exception{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql://localhost/jdbcone";
String userName="root";
String passWord="root";
Connection conn = DriverManager.getConnection(url, userName, passWord);
Statement stmt = null;
ResultSet rs = null;
//创建集合对象
List<Account> list=new ArrayList<>();
try {
//定义sql
String sql="select * from users";
//创建statement对象
stmt = conn.createStatement();
//执行sql语句
rs = stmt.executeQuery(sql);
//遍历rs中的数据
while (rs.next()){
Account account=new Account();
int id = rs.getInt("id");
String name = rs.getString("username");
String password = rs.getString("password");
String email= rs.getString("email");
//赋值
account.setId(id);
account.setUsername(name);
account.setPassword(password);
account.setEmail(email);
//存入集合
list.add(account);
// [Account{id=1, username='小马哥', password='888', email='[email protected]'}, Account{id=2, username='小飞侠',
// password='666', email='[email protected]'}, Account{id=3, username='马奎斯', password='333', email='[email protected]'}]
}
//打印集合
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}
//释放资源
rs.close();
stmt.close();
conn.close();
}
}
package com.study;
import java.sql.*;
public class PreparedStatementLogin {
public static void main(String[] args) throws Exception {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
String url="jdbc:mysql://localhost/jdbcone";
String userName="root";
String passWord="root";
Connection conn = DriverManager.getConnection(url, userName, passWord);
//接收用户输入的用户名和密码
String name="king";
String psw="888";
String sql="select * from users where username=? and password=?";
//创建pstmt对象
PreparedStatement pstmt = conn.prepareStatement(sql);
//设置值
pstmt.setString(1,name);
pstmt.setString(2,psw);
//执行sql语句
ResultSet rs = pstmt.executeQuery();
//判断是否登录成功
if (rs.next()){
System.out.println("登录成功!");
}
else {
System.out.println("登录失败!");
}
rs.close();
pstmt.close();
conn.close();
}
}
//登录成功!
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///users
username=root
password=root
# 初始化连接数
initialSize=5
# 最大连接数
maxActive=10
# 最大等地时间
maxWait=3000
package com.druidTest;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javafx.scene.layout.Priority;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;
public class DruidTest {
public static void main(String[] args) throws Exception {
//导入jar包
//定义配置文件
//加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("JDBCTest/src/druid.properties"));
//获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//获取数据库连接Connection
Connection conn = dataSource.getConnection();
System.out.println(conn);
}
}