Java 数据库添加,修改和删除
生活随笔
收集整理的這篇文章主要介紹了
Java 数据库添加,修改和删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
都是用executeUpdate方法
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException;import com.mysql.jdbc.Statement;public class JDbc {public static void main(String[] args) {Connection connection = null;Statement statement = null;try {Class.forName("com.mysql.jdbc.Driver");//加載Driver類connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test02","root","root");//鏈接數據庫statement = (Statement) connection.createStatement();//新建Statement接口String sql1 = "insert into tb_peo(id, name, birthday) values('5','郭珂','1999-8-10')";//insert into 表名(表中列名1,列名2,...) values('',''....)int a = statement.executeUpdate(sql1);System.out.println("表中有"+a+"行被修改");String sql2 = "update tb_peo set birthday = '1999-8-11'where id = 5";//update 表名 set 列名 = '修改值' where = ;int b = statement.executeUpdate(sql2);System.out.println("表中有"+b+"行被修改");String sql3 = "delete from tb_peo where id = 5";//delete from 表名 where = ;int c = statement.executeUpdate(sql3);System.out.println("表中有"+c+"行被修改");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {statement.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}} }?
?
總結
以上是生活随笔為你收集整理的Java 数据库添加,修改和删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASCII码与10进制、16进制转换对照
- 下一篇: java美元兑换,(Java实现) 美元