欢迎光临散文网 会员登陆 & 注册

MySQL作业:JDBC,增删改查,预处理,导jdbc包的方法,Date,datetime【诗书画唱】

2020-08-24 20:57 作者:诗书画唱  | 我要投稿

一般导入包

习惯

之后方便一起打包给对方,可以直接运行。

包名有时习惯为com.(公司名),由“www.(公司名).com”得来。



时间戳(timestamp),一个能表示一份数据在某个特定时间之前已经存在的、 完整的、 可验证的数据,通常是一个字符序列,唯一地标识某一刻的时间。使用数字签名技术产生的数据, 签名的对象包括了原始文件信息、 签名参数、 签名时间等信息。广泛的运用在知识产权保护、 合同签字、 金融帐务、 电子报价投标、 股票交易等方面。



1、创建一个商品信息表,包含id,name,price,createdate(生产日期,datetime类型)四个列,通过jdbc编程给商品信息表添加三条不同的记录,注意生产日期字段的处理。

create table sp(

id int primary key auto_increment,

name varchar(100) not null unique,

price float,

createdate datetime


);





用JDBC新增1条SQL的数据:

用JDBC新增10条SQL的数据:


package jy;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;

import java.util.Random;


public class Demo {


public static void main(String[] args) {


//下面是驱动名,是一个类:

String driverName = "com.mysql.jdbc.Driver";


//下面的localhost是ip地址,表示连接的是我自己电脑上的数据库。

//3306是端口号,不需要改变。

//Jj190802是需要连接的数据库的名字。

//?useUnicode=true&characterEncoding=UTF-8是用于中文乱码处理的部分。

String url = "jdbc:mysql://127.0.0.1:3306"

+ "/J190802?useUnicode="

+ "true&characterEncoding=UTF-8";

String user = "root";

String pwd = "root";

// :一般设置MySQL密码为root,防止忘了。

// ————————

//下面的Connection是获取负责java和数据库进行联系的“中间人”。

Connection con = null;

//下面是将java中的字符串变成一个真正能够执行的sql语句的类,预编译类:

PreparedStatement PStm1 = null;

// ——————

// 有时点Alt和/的键盘键,

// 就可以出现提示等,不用自己打完代码都可以

// 自动生成代码等。

// ————

//下面的ResultSet是查询结果集对象:

ResultSet RS = null;

try {

//下面的Class.forName是加载类驱动。

// (driverName)中的driverName

// 是驱动名:

Class.forName(driverName);//:类加载机制。


// 下面是连接url,user,pwd

// 的字符串:

con = DriverManager.getConnection(url,user,pwd);


// 下面是打印

// 连接url,user,pwd后

// 的一个大字符串:

System.out.println(con);


// 这条分割线上面的基本都是固定的。

// ——————————————————————————————————————————————————————

// 这条分割线下面的基本都是固定格式等的。

//新增SQL语句:

String sqlInsert1 = "insert into sp"

+ " (name,price,createdate) values(?,?,?)";

////将sql1字符串变成一个真正能够执行的sql语句(预编译)

PStm1 = con.prepareStatement(sqlInsert1);

// //设置占位符的内容,确定你要往数据库中插入什么数据

// //下标是从1开始的 :

String name;

// String sex;

int he=0;


for(int i=1;i<=10;i++){

name= "诗书画唱CD"+i;


// 下面是随机生成性别的方法,但这里用不着:

// Random r=new Random();

// int l=r.nextInt(2);

// if(l==0){

// sex="男";

// }else if(l==1){

// sex="女";

// }


PStm1.setString(1,name);


Date d = new Date();

//Date(得到当前时间)是java.util包下的一个类,打印出来的格式如下:

// Wed Sep 29 16:41:16 CST 2010


// //下面是将Date转换成java.sql.Date,


// getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。

java.sql.Date birth = new java.sql.Date(d.getTime());

//pstm.setString(2, "1998-7-25");

PStm1.setDate(3, birth);

PStm1.setFloat(2, 99.5f);



// //下面是用executeUpdate()执行sql语句。

// count用于判断是否执行成功:

int count = PStm1.executeUpdate();


if(count == 0) {

System.out.println("插入失败");

} else {

System.out.println("成功插入了" + count + "条数据");

}


he++;

}




System.out.println("共成功插入了" + he + "条数据");









//——————————————————————————————————————————————————————

// 这条分割线下面的基本都是不变等的。

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//下面是清理资源(先打开的后关闭,这是为了防止

// 连接过多,服务器崩了):

try {

if(PStm1 != null) {

PStm1.close();

}

if(con != null) {

con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}




2、在stuinfo表中添加十条不同的数据。

package jdbcMySQL;


import java.sql.*;

import java.util.Date;




public class insertStuinfo {


public static void main(String[] args) {


String driverName = "com.mysql.jdbc.Driver";


String url = "jdbc:mysql://127.0.0.1:3306"

+ "/j190802?useUnicode=true&"

+ "characterEncoding=UTF-8";

String user = "root";

String pwd = "root";


Connection Con = null;


PreparedStatement Pre = null;


ResultSet Res = null;

try {


Class.forName(driverName);

Con = DriverManager.getConnection(url,user,pwd);

System.out.println(Con);



String sql = "insert into stuinfo (name,banJi,birthday)"

+ "values(?,?,?)";

//将sql字符串变成一个真正能够执行的sql语句。

//用prepareStatement(预编译),可以防止注入,

//就是当prepareStatement不用时,

//有人恶意乱输入密码(比如“"”等双引号,让SQL

// 语句处报错。)

Pre = Con.prepareStatement(sql);



int he=0;

String  name;


for(int i=1;i<=10;i++){

Pre.setString(1,"诗书画唱"+i);


Date d = new Date();


java.sql.Date DateSQL = new java.sql.Date(d.getTime());


Pre.setDate(3, DateSQL);


Pre.setString(2,"诗书画唱班"+i);


int count = Pre.executeUpdate();

if(count == 0) {

System.out.println("插入失败");

} else {

System.out.println("成功插入了" + count + "条数据");

}

he++;

}


System.out.println("共成功插入了" +he

+ "条数据");


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源(先打开的后关闭):

try {

if(Pre != null) {

Pre.close();

}

if(Con != null) {

Con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}


3、删除stuinfo表中id为2和8的数据(一个sql语句)

一个sql语句的方法:

package jdbcMySQL;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;


public class deleteStudentOne {


public static void main(String[] args) {


String driverName = "com.mysql.jdbc.Driver";


String url = "jdbc:mysql://127.0.0.1:3306/j190802"

+ "?useUnicode=true&characterEncoding=UTF-8";

String user = "root";

String pwd = "root";


Connection Con = null;


PreparedStatement Pre = null;


ResultSet Res = null;

try {


Class.forName(driverName);

Con = DriverManager.getConnection(url,user,pwd);

System.out.println(Con);


//下面是删除的SQL的语句等:

String sql = "delete from stuinfo "

+ "where id = ? or id=?";

Pre = Con.prepareStatement(sql);

// ——————————

Pre.setInt(1, 2);


Pre.setInt(2, 8);

int count = Pre.executeUpdate();

if(count > 0) {

System.out.println("成功删除id为8,id为2的数据");

} else {

System.out.println("没有删除成功");

}


// ——————

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源(先打开的后关闭)

try {

if(Pre != null) {

Pre.close();

}

if(Con != null) {

Con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}






两个SQL语句的方法:

package jdbcMySQL;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;


public class deleteStuinfo {


public static void main(String[] args) {


String driverName = "com.mysql.jdbc.Driver";


String url = "jdbc:mysql://127.0.0.1:3306/j190802"

+ "?useUnicode=true&characterEncoding=UTF-8";

String user = "root";

String pwd = "root";


Connection Con = null;


PreparedStatement Pre = null;


ResultSet Res = null;

try {


Class.forName(driverName);

Con = DriverManager.getConnection(url,user,pwd);

System.out.println(Con);


//下面是删除的SQL的语句等:

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

Pre = Con.prepareStatement(sql);

// ——————————

Pre.setInt(1, 2);

int count = Pre.executeUpdate();

if(count > 0) {

System.out.println("成功删除id为2的数据");

} else {

System.out.println("没有删除成功");

}

//

// ——————————

Pre.setInt(1, 8);

int count2 = Pre.executeUpdate();

if(count2 > 0) {

System.out.println("成功删除id为8的数据");

} else {

System.out.println("没有删除成功");

}


// ——————

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源(先打开的后关闭)

try {

if(Pre != null) {

Pre.close();

}

if(Con != null) {

Con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}





4、修改stuinfo表中id为5的记录,将班级改成J190801,生日改成2001-3-21


package jdbcMySQL;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;


public class updateStuinfo {


public static void main(String[] args) {


String driverName = "com.mysql.jdbc.Driver";


String url = "jdbc:mysql://127.0.0.1:3306"

+ "/j190802?useUnicode="

+ "true&characterEncoding=UTF-8";

String user = "root";

String pwd = "root";


Connection Con = null;


PreparedStatement Pre = null;


ResultSet Res= null;

try {


Class.forName(driverName);

Con = DriverManager.getConnection(url,user,pwd);

System.out.println(Con);



String sql = "update stuinfo set name = ?,"

+ "birthday = ?,banJi= ? where id = ?";


Pre = Con.prepareStatement(sql);

//用setXXX方法设置占位符(?)的值,

// 记得要和sql语句中的占位符对应。

Pre.setString(1, "诗书画唱");

Pre.setString(2, "2001-3-21");

Pre.setString(3, "J1901班");

Pre.setInt(4, 5);

//下面是执行修改的executeUpdate()语句:

int count = Pre.executeUpdate();

System.out.println(count );

// execute 英[ˈeksɪkjuːt]

// 美[ˈeksɪkjuːt]

// v. (尤指依法) 处决,处死; 

// 实行; 执行; 实施; 成功地完成(技巧或动作);


if(count > 0) {

System.out.println("成功修改了" + count + "条数据");

} else {

System.out.println("修改失败");

}


} catch (Exception e) {


e.printStackTrace();

} finally {


try {

if(Pre != null) {

Pre.close();

}

if(Con != null) {

Con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}





5、查询商品信息表中的所有数据并打印出来

package jdbcMySQL;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;


public class selectSP {


public static void main(String[] args) {


String driverName = "com.mysql.jdbc.Driver";


String url = "jdbc:mysql:"

+ "//127.0.0.1:3306/"

+ "j190802?useUnicode"

+ "=true&characterEncoding=UTF-8";

String user = "root";

String pwd = "root";


Connection con = null;


PreparedStatement pre = null;


ResultSet RS = null;

try {


Class.forName(driverName);

con = DriverManager.getConnection(url,user,pwd);

System.out.println(con);


//下面是查询所有的SQL语句:

String sqlSelect = "select * from sp";

pre = con.prepareStatement(sqlSelect);

//执行查询:查询跟新增修改删除调用的方法不一样,要用上

// ResultSet 声明为结果集类型的变量RS(也可以取其中的

// 前三个字母取名为res):

RS = pre.executeQuery();

//RS中会存放查询出来的所有数据:

    System.out.println("下面是第一种遍历的方法: ");

while(RS.next()) {




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


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

float price=RS.getFloat("price");

Date  createdate=RS.getDate("createdate");

                System.out.println("商品编号:"+id+";"

                + "商品名称:"+name+";"

                + "商品价格:"+

price+"商品生产日期:"+createdate);

                

 

                

}

//因为RS = pre.executeQuery();执行多少遍

//,才会有多少遍的作用

//,所以下面要再次声明,同时执行下RS = pre.executeQuery();


   

// 下面遍历结果集合的话也可以用上

// getObject:

RS = pre.executeQuery();

            System.out.println("下面是第二种遍历的方法: ");

            while(RS.next()) {

        System.out.println(RS.getObject(1)+

"\t"+RS.getObject(2)+"\t"

+RS.getObject(3)+"\t");}

            

            

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源(先打开的后关闭)

try {

if(pre != null) {

pre.close();

}

if(con != null) {

con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}




6、查询stuinfo表中的数据并打印出来


package jdbcMySQL;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Date;


public class selectStudent {


public static void main(String[] args) {


String driverName = "com.mysql.jdbc.Driver";


String url = "jdbc:mysql:"

+ "//127.0.0.1:3306/"

+ "j190802?useUnicode"

+ "=true&characterEncoding=UTF-8";

String user = "root";

String pwd = "root";


Connection con = null;


PreparedStatement pre = null;


ResultSet RS = null;

try {


Class.forName(driverName);

con = DriverManager.getConnection(url,user,pwd);

System.out.println(con);


//下面是查询所有的SQL语句:

String sql = "select * from  stuinfo";

pre = con.prepareStatement(sql);

//执行查询:查询跟新增修改删除调用的方法不一样,要用上

// ResultSet 声明为结果集类型的变量RS(也可以取其中的

// 前三个字母取名为res):

RS = pre.executeQuery();

//RS中会存放查询出来的所有数据:

 

            while(RS.next()) {

        System.out.println(RS.getObject(1)+

"\t"+RS.getObject(2)+"\t"

+RS.getObject(3)

+"\t"

+RS.getObject(4)+"\t");}

            

            

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//清理资源(先打开的后关闭)

try {

if(pre != null) {

pre.close();

}

if(con != null) {

con.close();

}

} catch(Exception e) {

e.printStackTrace();

}

}

}


}




MySQL作业:JDBC,增删改查,预处理,导jdbc包的方法,Date,datetime【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律