JDBC實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查功能
JDBC,簡(jiǎn)單點(diǎn)來(lái)說(shuō),就是用Java操作數(shù)據(jù)庫(kù),下面簡(jiǎn)單介紹怎么實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查功能。
1、添加數(shù)據(jù)
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo2 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//1、注冊(cè)驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//2、定義sql
String sql = "insert into course values(?,?,?)";
//3、獲取Connection對(duì)象
//student表示你要操作的數(shù)據(jù)庫(kù)
//如果是locakhost:3306,也可以簡(jiǎn)寫(xiě)為"jdbc:mysql:///student"
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
//4、獲取執(zhí)行sql的對(duì)象
preparedStatement = connection.prepareStatement(sql);
//傳入?yún)?shù)
preparedStatement.setInt(1,5);
preparedStatement.setString(2,"JavaWeb");
preparedStatement.setInt(3,88);
//5、執(zhí)行sql
int count = preparedStatement.executeUpdate();
//6、處理結(jié)果
System.out.println(count);
if (count > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失敗");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7、釋放資源
//避免空指針異常
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
2、刪除數(shù)據(jù)
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo4 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//1、注冊(cè)驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//2、獲取連接對(duì)象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
//3、定義sql
String sql = "delete from course where cno = ?";
//4、獲取執(zhí)行sql對(duì)象
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,5);
//5、執(zhí)行sql
int count = preparedStatement.executeUpdate();
//6、處理結(jié)果
System.out.println(count);
if (count > 0) {
System.out.println("刪除成功");
} else {
System.out.println("刪除失敗");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7、釋放資源
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3、修改數(shù)據(jù)
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo3 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//1、注冊(cè)驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//2、獲取連接對(duì)象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
//3、定義sql
String sql = "update course set period = ? where cno = ?";
//4、獲取執(zhí)行sql對(duì)象
preparedStatement = connection.prepareStatement(sql);
//設(shè)置參數(shù)
preparedStatement.setInt(1,90);
preparedStatement.setInt(2,1);
//5、執(zhí)行sql
int count = preparedStatement.executeUpdate();
//6、處理結(jié)果
System.out.println(count);
if (count > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失敗!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7、釋放資源
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
4、查詢數(shù)據(jù)
package cn.itcast.jdbc;
import cn.itcast.domain.Course;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCDemo5 {
/**
* 查詢所有Course對(duì)象
* @return
*/
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Course> list = null;
try {
//1、注冊(cè)驅(qū)動(dòng)
Class.forName("com.mysql.jdbc.Driver");
//2、獲取連接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
//3、定義sql
String sql = "select * from course";
//4、獲取執(zhí)行sql的對(duì)象
preparedStatement = connection.prepareStatement(sql);
//5、執(zhí)行sql
resultSet = preparedStatement.executeQuery();
//6、遍歷結(jié)果集,封裝對(duì)象,裝載集合
Course course = null;
list = new ArrayList<Course>();
while (resultSet.next()) {
//獲取數(shù)據(jù)
int cno = resultSet.getInt("cno");
String cname = resultSet.getString("cname");
int period = resultSet.getInt("period");
//創(chuàng)建Course對(duì)象并賦值
course = new Course();
course.setCno(cno);
course.setCname(cname);
course.setPeriod(period);
//裝載集合
list.add(course);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
System.out.println(list);
}
}
我們可以發(fā)現(xiàn),增刪改的操作基本都是差不多的語(yǔ)句,且執(zhí)行sql的語(yǔ)句都是一樣的,都是preparedStatement.executeUpdate()。但查詢操作就有所不同了,返回的是一個(gè)結(jié)果集,且執(zhí)行sql的語(yǔ)句就是preparedStatement.executeQuery()。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn)
這篇文章主要介紹了Java使用Freemarker頁(yè)面靜態(tài)化生成的實(shí)現(xiàn),頁(yè)面靜態(tài)化是將原來(lái)的動(dòng)態(tài)網(wǎng)頁(yè)改為通過(guò)靜態(tài)化技術(shù)生成的靜態(tài)網(wǎng)頁(yè),FreeMarker?是一個(gè)用?Java?語(yǔ)言編寫(xiě)的模板引擎,它基于模板來(lái)生成文本輸,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06
Netty進(jìn)階之EventExecutorGroup源碼詳解
這篇文章主要介紹了Netty進(jìn)階之EventExecutorGroup源碼詳解,EventExecutorGroup繼承了JDK的ScheduledExecutroService,那么它就擁有了執(zhí)行定時(shí)任務(wù),執(zhí)行提交的普通任務(wù),需要的朋友可以參考下2023-11-11
JUnit中獲取測(cè)試類及方法的名稱實(shí)現(xiàn)方法
這篇文章主要介紹了JUnit中獲取測(cè)試類及方法的名稱實(shí)現(xiàn)方法,本文使用了JUnit中提供的TestName實(shí)現(xiàn),不過(guò)還有一些編程細(xì)節(jié)需要注意,需要的朋友可以參考下2015-06-06
解決JAVA8 Collectors.toMap value為null報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了解決JAVA8 Collectors.toMap value為null報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Java?靜態(tài)代理與動(dòng)態(tài)代理解析
這篇文章主要介紹了Java?靜態(tài)代理與動(dòng)態(tài)代理解析,關(guān)于靜態(tài)代理與動(dòng)態(tài)代理,一直是比較困擾很多新人開(kāi)發(fā),但實(shí)際我們開(kāi)發(fā)中,小到寫(xiě)的某個(gè)工具類,大到經(jīng)常使用的Retrofit?其內(nèi)部都使用了動(dòng)態(tài)代理,所以這篇文章從基礎(chǔ)到源碼解析,以便簡(jiǎn)單理解靜態(tài)代理與Jdk中的動(dòng)態(tài)代理2022-02-02

