第一种比较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. Keyboard的显示与隐藏

    一个控制键盘显示与隐藏的工具类分享给大家 public class KeyBoardTool { /** * 假设输入法在窗体上已经显示.则隐藏.反之则显示 * @param context */ p ...

  2. Cocos2d-x 动手实现游戏主循环

    因为Cocos2d-x封装的非常好,所以对于非常多新手,他们仅仅知道先new一个场景,在场景上加入布景或精灵,然后用Director的runWithScene便能够执行游戏了.假设给一个精灵加个动作, ...

  3. 继承的综合运用《Point类派生出Circle类而且进行各种操作》

    类的组合与继承 (1)先建立一个Point(点)类.包括数据成员x,y(坐标点). (2)以Point为基类.派生出一个Circle(圆)类,添加数据成员(半径),基类的成员表示圆心: (3)编写上述 ...

  4. cmd 进入mysql 小技巧

    1.開始中找出执行:输入cmd 2.查找appserv所在盘,我的在D盘.所以接着输入:d: 3.在d盘中查找mysql所在文件夹:cd appserv\mysql\bin 4.再输入主机名.数据库名 ...

  5. 2015.03.12,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯”学习笔记 SESSION 25

    1.about keeping one's mouth shut taciturn,名词形式taciturnity,沉默寡言. 美国第30任总统库里奇,以沉默寡言著称.他来自新英格兰,那里视tacit ...

  6. 【POJ 1733】 Parity Game

    [题目链接] http://poj.org/problem?id=1 [算法] 并查集 [代码] #include <algorithm> #include <bitset> ...

  7. 高德SDK获取到的坐标转换为GPS真实坐标方法,Java版

    发现高德SDK不提供高德的坐标转GPS坐标(GCJ_02转WGS_84),下面是一份Java版的 /**************************** 文件名:GCJ2WGS.java 创建时间 ...

  8. bootstrap-导航条

    默认样式的导航条 导航条是在您的应用或网站中作为导航页头的响应式基础组件.它们在移动设备上可以折叠(并且可开可关),且在视口(viewport)宽度增加时逐渐变为水平展开模式. 两端对齐的导航条导航链 ...

  9. iOS9 & Xcode7 下设置LaunchImage启动图片 问题及解决

    最近在学习iOS开发,碰到一个设置启动图片的问题,怎么也搞不定,综合网上种种资料后Done,现在把完整过程写一下. 这里以建立一个空的Single View Application 为演示基础. 1. ...

  10. CF #487 (Div. 2) D. A Shade of Moonlight 构造_数形结合

    题意: 给 nnn个长度为 lll 且互不相交的开区间 (xi,xi+l)(x_{i}, x_{i}+l)(xi​,xi​+l) ,每个区间有一个移动速度 vvv,v∈1,−1v∈1,-1v∈1,−1 ...