Java第十九章-数据库操作
作者:互联网
绪论
//数据库系统是由数据库、数据库管理系统、应用系统、数据库管理员构成的。
连接数据库
//通过创建的getConnection()方法,获取与MySQL数据库的连接。
package 数据库操作;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn {
//声明Connection对象
Connection conn;
public Connection getConnection() {
//加载数据库驱动类
try {
Class.forName("com.myaql.jbdc.Driver");
System.out.println("数据库加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//通过访问数据库的URL,获取数据库连接对象
try {
conn = DriverManager.getConnection("jdbc.mysql:" +
"//127.0.0.1:3306/test","root", "123456");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]) {
Conn c = new Conn();
c.getConnection();
}
}
顺序查询
//将数据表中的数据检索出来,保存在遍历查询结果集ResultSet中,并遍历该结果集
package 数据库操作;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Gradation {
//声明Connection类对象
static Connection con;
//声明Statement类对象
static Statement sql;
//声明ResultSet类对象
static ResultSet res;
public Connection getConnection() {
//加载数据库驱动类
try {
Class.forName("com.myaql.jbdc.Driver");
System.out.println("数据库加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//通过访问数据库的URL,获取数据库连接对象
try {
con = DriverManager.getConnection("jdbc.mysql:" +
"//127.0.0.1:3306/test","root", "123456");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static void main(String args[]) {
//创建本类对象
Gradation c = new Gradation();
//与数据连接
con = c.getConnection();
try {
//实例化Statement对象
sql = con.createStatement();
//执行SQL语句,返回结果值
res = sql.executeQuery("select","from tb_stu");
//如果当前语句不是最后一条,进入循环
while (res.next()) {
//获取列名是id的字段值
String id = res.getString("id");
//获取列名是name的字段值
String name = res.getString("name");
//获取列名是sex的字段值
String sex = res.getString("sex");
//获取列名是birthday的字段名
String birthday = res.getString("birthday");
//将列值输出
System.out.println("编号:" + id);
System.out.println("姓名" + name);
System.out.println("性别:" + sex);
System.out.println("生日:" + birthday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
模糊查询
//将姓张的同学信息检索出来
package 数据库操作;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Train {
//声明Connection类对象
static Connection con;
//声明Statement类对象
static Statement sql;
//声明ResultSet类对象
static ResultSet res;
public Connection getConnection() {
//加载数据库驱动类
try {
Class.forName("com.myaql.jbdc.Driver");
System.out.println("数据库加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//通过访问数据库的URL,获取数据库连接对象
try {
con = DriverManager.getConnection("jdbc.mysql:" +
"//127.0.0.1:3306/test","root", "123456");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static void main(String args[]) {
//创建本类对象
Train c = new Train();
//与数据连接
con = c.getConnection();
try {
//实例化Statement对象
sql = con.createStatement();
//执行SQL语句,返回结果值
res = sql.executeQuery("select","from tb_stu where" + "name like '张%'");
//如果当前语句不是最后一条,进入循环
while (res.next()) {
//获取列名是id的字段值
String id = res.getString("id");
//获取列名是name的字段值
String name = res.getString("name");
//获取列名是sex的字段值
String sex = res.getString("sex");
//获取列名是birthday的字段名
String birthday = res.getString("birthday");
//将列值输出
System.out.println("编号:" + id);
System.out.println("姓名" + name);
System.out.println("性别:" + sex);
System.out.println("生日:" + birthday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
预处理语句
//使用预处理语句可以使应用程序更容易的动态改变SQL语句中关于字段值条件的设定
package 数据库操作;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Prep {
//声明各类对象
static Connection con;
static PreparedStatement sql;
static ResultSet res;
//与数据库连接的方法
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:" +
"//127.0.0.1:3306/test","root","123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String args[]) {
//创建本类对象
Prep c = new Prep();
//获取与数据库的连接
con = c.getConnection();
try {
//实例化与处理对象
sql = con.prepareStatement("select * from table_name " +
" where id = ?");
//设置参数
sql.setInt(1, 19);
//执行预处理语句
res = sql.executeQuery();
//如果当前记录不是聚合集中的最后一行,则进入循环体
while (res.next()) {
//获取结果列中第一列的值
String id = res.getString(1);
//获取name、sex、birthday的列值
String name = res.getString("name");
String sex = res.getString("sex");
String birthday = res.getString("birthday");
//输出信息
System.out.println("编号:" + id);
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("生日:" + birthday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加、修改、删除记录
//完成以上功能可以通过PreparedStatement类得指定参宿动态的数据表种的元素进行修改,并通过executeUpdate()方法执行更新后的语句
package 数据库操作;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Renewal {
//声明各类对象
static Connection con;
static PreparedStatement sql;
static ResultSet res;
//与数据库连接的方法
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql:" +
"//127.0.0.1:3306/test","root","123456");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String args[]) {
//实例化本类对象
Renewal c = new Renewal();
//调用连接数据库的方法
con = c.getConnection();
try {
//查询数据库
sql = con.prepareStatement("select * from tb_stu");
//执行SQL语句
res = sql.executeQuery();
System.out.println("执行添加、修改、删除操作之前的数据");
while (res.next()) {
String id = res.getString("1");
String name = res.getString("name");
String sex = res.getString("sex");
String birthday = res.getString("birthday");
System.out.println("编号:" + id);
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("生日:" + birthday);
}
//预处理添加数据
sql = con.prepareStatement("insert into tb_stu" + "values(?,?,?)");
sql.setString(1,"张一");
sql.setString(2,"女");
sql.setString(3,"2012-12-1");
sql.executeUpdate();
//更新数据信息
sql = con.prepareStatement("update tb_stu birthday" +
"=? where id = (select min(id) from tb_stu)");
sql.setString(1,"2012-12-02");
sql.executeUpdate();
//删除数据
sql.executeUpdate("delete from tb_stu where id = " +
"(select min(id)from tb_stu)");
sql.setInt(1,1);
sql.executeUpdate();
//查询修改数据后tb_stu表中的数据
sql = con.prepareStatement("select * from tb_stu");
//执行SQL语句
res = sql.executeQuery();
System.out.println("执行增加、修改、删除后的数据");
while (res.next()) {
String id = res.getString("1");
String name = res.getString("name");
String sex = res.getString("sex");
String birthday = res.getString("birthday");
System.out.println("编号:" + id);
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("生日:" + birthday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Dragon Prince
发布了43 篇原创文章 · 获赞 2 · 访问量 969
私信
关注
标签:Java,String,res,数据库,System,第十九章,sql,println,out 来源: https://blog.csdn.net/wangzilong_2019/article/details/104196739