第一种比较low:用了statment,没有用preparedstatement。另外,插入时,不灵活,不能调用参数,但是如果直接给函数形参的话就会被SQL注入攻击,所以,最好在sql语句中使用?代表要引进的参数。

工具类(DBUtil类):用来连接和关闭数据库

 package JDBC;

 /**
* Created by Administrator on 2018/3/8 0008.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class DBUtil {
public static final String url="jdbc:mysql://127.0.0.1/bz?useSSL=false";
public static final String username="root";
public static final String password="root";
public static final String driver="com.mysql.jdbc.Driver";
public static Connection DBcon(){
Connection con=null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con= DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
} public static void close(ResultSet rs,Statement stat,Connection conn){ try {
if(stat!=null)
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

测试类(Test类):其中有增删改查的功能,在测试类中写下sql语句,进行测试

 package JDBC;

 /**
* Created by Administrator on 2018/3/8 0008.
*/
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static Connection conn=null;
public static Statement stat=null;
public static ResultSet rs=null;
public static void main(String[] args){
String select="select * from father;";
String insert="insert into father values(null,'POP');";
String update="update father set f_name='haha' where f_name='POP';";
String delete="delete from father where f_name='haha';";
conn=DBUtil.DBcon();
select(select);
insert(insert);
select(select);
update(update);
select(select);
delete(delete);
select(select);
DBUtil.close(rs,stat, conn);
}
public static void select(String quary){
try {
stat=conn.createStatement();
rs=stat.executeQuery(quary);
while(rs.next()){
System.out.println(rs.getObject("fid")+"|"+rs.getObject("f_name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insert(String quary){
try {
stat=conn.createStatement();
int i=stat.executeUpdate(quary);
System.out.println("插入"+i+"行");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void delete(String quary){
try {
stat=conn.createStatement();
int i=stat.executeUpdate(quary);
System.out.println("删除了"+i+"行");
} catch (SQLException e) {
e.printStackTrace();
} }
public static void update(String quary){
try {
stat=conn.createStatement();
int i=stat.executeUpdate(quary);
System.out.println("更改"+i+"行");
} catch (SQLException e) {
e.printStackTrace();
}
}
}

第二种解决了第一种low方法的问题,并且还新增了方法调用和过程调用:使用了CallableStatement类

package com.weikun.jdbc;

import jdk.internal.org.objectweb.asm.Type;
import org.junit.Test; import java.sql.*; /**
* Created by Administrator on 2018/3/12 0012.
*/
public class C {
@Test
public void testFun(){
Connection conn = null;
CallableStatement cs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false", "root", "root");
cs=conn.prepareCall("{?=call f_1(?)}");
cs.registerOutParameter(1, Type.DOUBLE);
cs.setDouble(2,0.3); cs.execute();
System.out.println(cs.getObject(1));
}catch(Exception e){
e.printStackTrace();
}
} @Test
public void testProd(){
Connection conn = null;
CallableStatement cs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false", "root", "root");
cs=conn.prepareCall("call p_1(?,?,?,?);");
cs.setInt(1,20005);
cs.setBoolean(2,true);
cs.setDouble(3,0.2);
cs.registerOutParameter(4, Types.FLOAT);
cs.execute();//返回的是第一个返回参数是不是个结果集,是的话返回1,不是的话返回0
System.out.println(cs.getObject(4));
}catch (Exception e){
e.printStackTrace();
}finally {
if(cs!=null){
try {
cs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} @Test
public void add() {
Connection connection = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false", "root", "root");
ps=connection.prepareStatement("insert into e(v_name,v_salary) values(?,?)");
ps.setString(1,"jack");
ps.setDouble(2,3000);
System.out.println(ps.executeUpdate());
}catch (Exception e){
e.printStackTrace();
}finally {
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} @Test
public void del(){
Connection connection = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false", "root", "root");
ps=connection.prepareStatement("DELETE from e where v_id=?");
ps.setInt(1,2);
System.out.println(ps.executeUpdate());
}catch (Exception e){
e.printStackTrace();
}finally {
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} @Test
public void update(){
Connection connection = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false", "root", "root");
ps=connection.prepareStatement("UPDATE e SET v_name=? WHERE v_id=?");
ps.setString(1,"jack");
ps.setInt(2,1);
System.out.println(ps.executeUpdate());
}catch (Exception e){
e.printStackTrace();
}finally {
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} @Test
public void quaryCon(){
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false","root","root");
ps=connection.prepareStatement("select * from products where prod_price>? and vend_id=?");
ps.setDouble(1,15.0);
ps.setInt(2,1003);
rs=ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString("prod_name"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} @Test
public void quaryAll(){ //查询所有结果集,打印其中的某几列
Connection connection=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");//1、加载驱动
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bz?useUnicode=true&charactorCoding=UTF-8&useSSL=false","root","root");
ps=connection.prepareStatement("select * from products");//尽量只用prepared态不使用Statment
rs=ps.executeQuery();//返回一个结果集
while(rs.next()){
System.out.println(rs.getObject("prod_id"));//从结果集中取出列名为prod_id的数据。
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
}

JDBC连接mysql增删改查整体代码的更多相关文章

  1. mysql数据库的连接以及增删改查Java代码实现(转载)

    每天叫醒自己的不是闹钟,而是梦想 数据库: create table t1(id int primary key not null auto_increment,name varchar(32),pa ...

  2. java 连接mysql增删改查

    1.创建mysql测试表 2.按下图创建3个文件夹与3个类 3.三个类的代码 PersionDao :目录(Data Access Object), 数据访问对象是第一个面向对象的数据库接口 pack ...

  3. MySQL—增删改查,分组,连表,limit,union,alter,排序,去重

    MySQL增删改查 在表格的增删改查中,查的内容是最多的,包括group by ,join,limit,union,alter,排序都是服务于查的 #sql语句数据行操作补充 #增加: #insert ...

  4. mysql增删改查相关操作

    mysql增删改查相关操作 以前用mysql用的少,对于数据库相关的操作不熟悉,现在开始要接触数据库了,记录一下相关的基础操作吧. 1.数据库的授权操作 # mysql -u root -p Ente ...

  5. 基于gin的golang web开发:mysql增删改查

    Go语言访问mysql数据库需要用到标准库database/sql和mysql的驱动.标准库的Api使用比较繁琐这里再引入另一个库github.com/jmoiron/sqlx. go get git ...

  6. mvc模式jsp+servel+jdbc oracle基本增删改查demo

    mvc模式jsp+servel+jdbc oracle基本增删改查demo 下载地址

  7. PHP MySql增删改查

    mysql_connect()连接数据库 mysql_select_db选择数据库 mysql_fetch_assoc()获取结果集 mysql_query()执行sql语句 实例如下: <?p ...

  8. mysql增删改查练习

    Mysql增删改查sql语句练习 关于数据库的一些操作: 进入mysql 命令行: mysql -uroot –p 查看所有数据库: show databases; 创建数据库: create dat ...

  9. Django学习之mysql增删改查

    上节介绍了如何使用命令行操作mysql增删改查,现在介绍如何使用python管理mysql 使用pip 下载完mysql后,mysql会以pymysql模块的形式存储在pycharm的包文件里.我们通 ...

随机推荐

  1. iOS-获取Model(设备型号)、Version(设备版本)、app(程序版本)等

    IOS-获取Model(设备型号).Version(设备版本).app(程序版本)等 NSLog(@"uniqueIdentifier: %@", [[UIDevice curre ...

  2. Android面试题集

    前几天整理了Java面试题集合,今天再来整理下Android相关的面试题集合.假设你希望能得到最新的消息,能够关注https://github.com/closedevice/interview-ab ...

  3. 原来C++之父在大摩工作呀,并且还是总经理。。

    摩根士丹利信息技术部门简历接收即将截止.请同学们抓紧投递 摩根士丹利9月.10月将在中国各大高校举办包含技术讲座.信息分享会以及校园宣讲会在 内的一系列校园活动.同学们将有机会和摩根士丹利高管以及返校 ...

  4. code::blocks配置编译cuda并进行第一个demo的測试

    我们先新建个NVCC的编译器. 使用复制GCC编译器的方式进行新建,然后我们进行下面的路径配置 先来看看链接库,将我们常常使用的cuda库链接进来. 然后链接cuda的头文件: 接着配置调试工具以及编 ...

  5. VS2013找不到SDKDDKVer.h

    今天在升级vs2010 的project的时候遇到了一个这种问题.提示:找不到SDKDKVer.h 通过查找资料发现,原来是vs版本号之间Windows SDK的路径宏定义不同,有些坑. 网上有人说能 ...

  6. 我的Android进阶之旅------&gt;android Button上面的英文字符串自己主动大写的问题解决

    今天碰到一个关于Button的问题:android Button上面的英文字符串会自己主动变成大写,执行的Android 5.1版本号,例如以下图所看到的: 图1:Button 图2:TextView ...

  7. Gradle之依赖管理

    Gradle之依赖管理 泡在网上的日子 / 文 发表于2015-01-29 16:12 第8824次阅读 Gradle,Android Studio 2 编辑推荐:稀土掘金,这是一个针对技术开发者的一 ...

  8. sql server 数据库展开变慢

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/99bbcb47-d4b5-4ec0-9e91-b1a23a655844/ssms-2 ...

  9. 机器学习之线性分类器(Linear Classifiers)——肿瘤预测实例

    线性分类器:一种假设特征与分类结果存在线性关系的模型.该模型通过累加计算每个维度的特征与各自权重的乘积来帮助决策. # 导入pandas与numpy工具包. import pandas as pd i ...

  10. USACO 1.5 Superprime Rib

    Superprime Rib Butchering Farmer John's cows always yields the best prime rib. You can tell prime ri ...