MySQL作業(yè):JDBC,增刪改查,預處理,導jdbc包的方法,Date,datetime【詩書畫唱】
一般導入包
習慣

之后方便一起打包給對方,可以直接運行。
包名有時習慣為com.(公司名),由“www.(公司名).com”得來。



時間戳(timestamp),一個能表示一份數(shù)據(jù)在某個特定時間之前已經(jīng)存在的、 完整的、 可驗證的數(shù)據(jù),通常是一個字符序列,唯一地標識某一刻的時間。使用數(shù)字簽名技術(shù)產(chǎn)生的數(shù)據(jù), 簽名的對象包括了原始文件信息、 簽名參數(shù)、 簽名時間等信息。廣泛的運用在知識產(chǎn)權(quán)保護、 合同簽字、 金融帳務(wù)、 電子報價投標、 股票交易等方面。
1、創(chuàng)建一個商品信息表,包含id,name,price,createdate(生產(chǎn)日期,datetime類型)四個列,通過jdbc編程給商品信息表添加三條不同的記錄,注意生產(chǎn)日期字段的處理。
create table sp(
id int primary key auto_increment,
name varchar(100) not null unique,
price float,
createdate datetime
);

用JDBC新增1條SQL的數(shù)據(jù):

用JDBC新增10條SQL的數(shù)據(jù):
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) {
//下面是驅(qū)動名,是一個類:
String driverName = "com.mysql.jdbc.Driver";
//下面的localhost是ip地址,表示連接的是我自己電腦上的數(shù)據(jù)庫。
//3306是端口號,不需要改變。
//Jj190802是需要連接的數(shù)據(jù)庫的名字。
//?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";
// :一般設(shè)置MySQL密碼為root,防止忘了。
// ————————
//下面的Connection是獲取負責java和數(shù)據(jù)庫進行聯(lián)系的“中間人”。
Connection con = null;
//下面是將java中的字符串變成一個真正能夠執(zhí)行的sql語句的類,預編譯類:
PreparedStatement PStm1 = null;
// ——————
// 有時點Alt和/的鍵盤鍵,
// 就可以出現(xiàn)提示等,不用自己打完代碼都可以
// 自動生成代碼等。
// ————
//下面的ResultSet是查詢結(jié)果集對象:
ResultSet RS = null;
try {
//下面的Class.forName是加載類驅(qū)動。
// (driverName)中的driverName
// 是驅(qū)動名:
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字符串變成一個真正能夠執(zhí)行的sql語句(預編譯)
PStm1 = con.prepareStatement(sqlInsert1);
// //設(shè)置占位符的內(nèi)容,確定你要往數(shù)據(jù)庫中插入什么數(shù)據(jù)
// //下標是從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轉(zhuǎn)換成java.sql.Date,
// getTime() 方法可返回距 1970 年 1 月 1 日之間的毫秒數(shù)。
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()執(zhí)行sql語句。
// count用于判斷是否執(zhí)行成功:
int count = PStm1.executeUpdate();
if(count == 0) {
System.out.println("插入失敗");
} else {
System.out.println("成功插入了" + count + "條數(shù)據(jù)");
}
he++;
}
System.out.println("共成功插入了" + he + "條數(shù)據(jù)");
//——————————————————————————————————————————————————————
// 這條分割線下面的基本都是不變等的。
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//下面是清理資源(先打開的后關(guān)閉,這是為了防止
// 連接過多,服務(wù)器崩了):
try {
if(PStm1 != null) {
PStm1.close();
}
if(con != null) {
con.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}





2、在stuinfo表中添加十條不同的數(shù)據(jù)。
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字符串變成一個真正能夠執(zhí)行的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 + "條數(shù)據(jù)");
}
he++;
}
System.out.println("共成功插入了" +he
+ "條數(shù)據(jù)");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源(先打開的后關(guān)閉):
try {
if(Pre != null) {
Pre.close();
}
if(Con != null) {
Con.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}


3、刪除stuinfo表中id為2和8的數(shù)據(jù)(一個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的數(shù)據(jù)");
} else {
System.out.println("沒有刪除成功");
}
// ——————
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源(先打開的后關(guān)閉)
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的數(shù)據(jù)");
} else {
System.out.println("沒有刪除成功");
}
//
// ——————————
Pre.setInt(1, 8);
int count2 = Pre.executeUpdate();
if(count2 > 0) {
System.out.println("成功刪除id為8的數(shù)據(jù)");
} else {
System.out.println("沒有刪除成功");
}
// ——————
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源(先打開的后關(guān)閉)
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方法設(shè)置占位符(?)的值,
// 記得要和sql語句中的占位符對應。
Pre.setString(1, "詩書畫唱");
Pre.setString(2, "2001-3-21");
Pre.setString(3, "J1901班");
Pre.setInt(4, 5);
//下面是執(zhí)行修改的executeUpdate()語句:
int count = Pre.executeUpdate();
System.out.println(count );
// execute 英[?eks?kju?t]
// 美[?eks?kju?t]
// v. (尤指依法) 處決,處死;?
// 實行; 執(zhí)行; 實施; 成功地完成(技巧或動作);
if(count > 0) {
System.out.println("成功修改了" + count + "條數(shù)據(jù)");
} 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、查詢商品信息表中的所有數(shù)據(jù)并打印出來
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);
//執(zhí)行查詢:查詢跟新增修改刪除調(diào)用的方法不一樣,要用上
// ResultSet 聲明為結(jié)果集類型的變量RS(也可以取其中的
// 前三個字母取名為res):
RS = pre.executeQuery();
//RS中會存放查詢出來的所有數(shù)據(jù):
? ? 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+"商品生產(chǎn)日期:"+createdate);
? ? ? ? ? ? ? ??
?
? ? ? ? ? ? ? ??
}
//因為RS = pre.executeQuery();執(zhí)行多少遍
//,才會有多少遍的作用
//,所以下面要再次聲明,同時執(zhí)行下RS = pre.executeQuery();
? ?
// 下面遍歷結(jié)果集合的話也可以用上
// 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 {
//清理資源(先打開的后關(guān)閉)
try {
if(pre != null) {
pre.close();
}
if(con != null) {
con.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}


6、查詢stuinfo表中的數(shù)據(jù)并打印出來
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);
//執(zhí)行查詢:查詢跟新增修改刪除調(diào)用的方法不一樣,要用上
// ResultSet 聲明為結(jié)果集類型的變量RS(也可以取其中的
// 前三個字母取名為res):
RS = pre.executeQuery();
//RS中會存放查詢出來的所有數(shù)據(jù):
?
? ? ? ? ? ? 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 {
//清理資源(先打開的后關(guān)閉)
try {
if(pre != null) {
pre.close();
}
if(con != null) {
con.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}



