当前位置:   article > 正文

java对数据库的增删改查_在java中对数据库进行增删改查

java答辩数据库增删改查

0750914a7a5eb9fd36ec259f185ccd5b.png

代码区域:

19cf92b28b6fc5172211836950514a9c.png

6064d96c5908303c353bbac07784361d.png

package com.oracle.jdbc.demo1;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class JDBCDemo {

//四个属性(四个常量的字符串)

/*

jdbcName

url

user

password

*/

private static final String jdbcName="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept";

private static final String user="root";

private static final String password="123456";

/*

* 一个类(DriverManeger)四个接口(Connection、)

* */

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

try {

Class.forName(jdbcName);

conn=DriverManager.getConnection(url, user, password);

//获得conn就表示获取了数据库的连接

System.out.println("连接数据库成功");

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

2.在java中向数据库添加数据

第一种方法:添加数据

350a93b831ff90bf7228b4acfd0bf795.png

代码区域:

82f2ccb307360a0bbebb520fefa03508.png 

5b5631e01748dc59f435fbb3eafad9f3.png

package com.oracle.jdbc.demo2;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCAdd {

private static final String jdbcName="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept";

private static final String user="root";

private static final String password="123456";

/*

* 一个类(DriverManeger)四个接口(Connection、PreparedStatement、)

* */

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

try {

Class.forName(jdbcName);

conn=DriverManager.getConnection(url, user, password);

//增加数据的操作

String name="田雨";

String sex="女";

String sql="insert into person values(null,‘"+name+"‘,‘"+sex+"‘)";

PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句

int i=pst.executeUpdate(); //返回成功插入数据的行数

System.out.println("成功添加了"+i+"条记录");

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

第二中方法:添加数据

代码区域:

d2e2a734b78a05fe08e2b9217d7eac55.png

package com.oracle.jdbc.demo2;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCAdd2 {

private static final String jdbcName="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept";

private static final String user="root";

private static final String password="123456";

/*

* 一个类(DriverManeger)四个接口(Connection、PreparedStatement、)

* */

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

try {

Class.forName(jdbcName);

conn=DriverManager.getConnection(url, user, password);

//增加数据的操作

String name="田雨2";

String sex="女";

String sql="insert into person values(null,?,?)";

PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句

pst.setString(1, name); //填充第1个问好

pst.setString(2, sex); //填充第2个问好

int i=pst.executeUpdate(); //返回成功插入数据的行数

System.out.println("成功添加了"+i+"条记录");

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

3.在java中修改数据库的内容

1cd899f068a2bd02ce2faa4daa3db79e.png

代码区域:

76805f457c0e6c6b635f584ab01dfc40.png

d69171a73526d70f6b12aecadff42c35.png

package com.oracle.jdbc.demo3;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCModify {

private static final String jdbcName="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept";

private static final String user="root";

private static final String password="123456";

/*

* 一个类(DriverManeger)四个接口(Connection、PreparedStatement、)

* */

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

try {

Class.forName(jdbcName);

conn=DriverManager.getConnection(url, user, password);

//修改数据的操作

int id=2;

String name="王希宝";

String sex="男";

String sql="update person set name=?,sex=? where id=?";

PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句

pst.setString(1, name); //填充第1个问好

pst.setString(2, sex); //填充第2个问好

pst.setInt(3, id);

int i=pst.executeUpdate(); //返回成功修改数据的行数

System.out.println("成功修改了"+i+"条记录");

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

4.在java中删除数据库的内容

1daf5a2aef3593136fb837fa07698709.png

代码区域:

7bf6114875225126ef64046ebf0be260.png

package com.oracle.jdbc.demo4;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class JDBCDel {

private static final String jdbcName="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept";

private static final String user="root";

private static final String password="123456";

/*

* 一个类(DriverManeger)四个接口(Connection、PreparedStatement、)

* */

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

try {

Class.forName(jdbcName);

conn=DriverManager.getConnection(url, user, password);

//删除数据的操作

int id=2;

String sql="delete from person where id=?";

PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句

pst.setInt(1, id);

int i=pst.executeUpdate(); //返回成功删除数据的行数

System.out.println("成功删除了"+i+"条记录");

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

5.在java中查看数据库的内容

8bfd8845f48ab71e004baf65f6cf27a9.png

代码区域:

ec6de42f0ac7637ad4a5dbeae060223d.png

package com.oracle.jdbc.demo5;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class JDBCFindAll {

private static final String jdbcName="com.mysql.jdbc.Driver";

private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept";

private static final String user="root";

private static final String password="123456";

/*

* 一个类(DriverManeger)四个接口(Connection、PreparedStatement、ResultSet、)

* */

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn=null;

try {

Class.forName(jdbcName);

conn=DriverManager.getConnection(url, user, password);

//查询数据的操作

String sql="select id,name,sex from person";

PreparedStatement pst=conn.prepareStatement(sql); //准备执行sql语句

ResultSet rs=pst.executeQuery();

while(rs.next()){

int id=rs.getInt("id");

String name=rs.getString("name");

String sex=rs.getString("sex");

System.out.println(id+" "+name+" "+sex);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

在java中对数据库进行增删改查

标签:cti   value   rac   drive   pass   sql   try   demo   常量

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:https://www.cnblogs.com/lxy151/p/8124829.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/798055
推荐阅读
相关标签
  

闽ICP备14008679号