目錄
環境配置
代碼
運作結果
環境配置
首先IDE我用的是Eclipse。
資料庫驅動用的(mysql-connector-java-8.0.29)。
驅動下載下傳位址
java項目使用驅動的過程是如下圖
1.點選工具欄的project
2.點選project欄目裡的Properties
3.點選Java Build Path進入如下界面
4.點選 Add External JARs 進入如下界面
5.選擇你下載下傳的驅動檔案(.jar)
6.出現如下界面代表此項目引用mysql驅動成功
代碼
書籍資訊表如下:
字段是:id,name,author,company,price
意思是:書号,書名,作者,出版社,價格
package test_jdbc;
import java.sql.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class connect {
//添加驅動
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//說明以下字元串含義
//jdbc:mysql:port port即mysql服務的端口号,預設是3306,如果不是需加上端口号
//localhost:資料庫所在ip(127.0.0.1)
//test :連結的資料庫名
static final String DB_URL = "jdbc:mysql://localhost/test";
// 資料庫名和密碼根據自身情況修改修改
static final String USER = "root";
static final String PASS = "1234";
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("請輸入使用者名:");
String user_id = scan.next();
System.out.println("\n請輸入密碼:");
String password = scan.next();
//通過person 表來通過mysql實作驗證登入
//person 字段為 id,password,level
// 使用者名,密碼,權限等級(1級為管理者權限)
//傳回值為 權限等級,傳回0 代表不存在此使用者
int level = check_person(user_id,password);
if(level>0)
{
while(true)
{
//主要功能如下
/*
* 普通使用者隻有檢視功能
* 隻有管理者才能修改、删除、和添加
*/
System.out.println("\n1:查詢\n"
+ "2:修改\n"
+ "3:删除\n"
+ "4:添加\n"
+ "5:退出");
System.out.println("請輸入指令:");
int code = scan.nextInt();
if(code==1){
System.out.println("請輸入相關資訊:");
String str = scan.next();
select_from(str);
}
else if(code==2){
if(level==1)
{
System.out.println("輸入書号:");
String id = scan.next();
System.out.println("輸入修改字段(id,name,author,company,price):");
String colname = scan.next();
if(colname.compareTo("price")==0)
{
System.out.println("輸入修改後的值:");
String price = scan.next();
if(check_price(price) == true)
{
update(id, colname, price);
}
else
{
System.out.println("價格輸入不符合規定,最多保留兩位小數");
}
}
else
{
System.out.println("輸入修改後的值:");
String price = scan.next();
update(id, colname, price);
}
}
else
{
System.out.println("權限不夠");
}
}
else if(code==3)
{
if(level==1){
System.out.println("\n輸入要删除的書的書号:");
String id = scan.next();
delete_from(id);
}
else
{
System.out.println("權限不夠");
}
}
else if(code==4)
{
if(level==1){
System.out.println("輸入書号:");
String id = scan.next();
System.out.println("輸入書名:");
String name = scan.next();
System.out.println("輸入作者:");
String author = scan.next();
System.out.println("輸入出版社:");
String company = scan.next();
System.out.println("輸入價格:");
String price = scan.next();
if(check_price(price)==true)
{
String[] str= {id,name,author,company,price};
insert_into(str);
}
else
{
System.out.println("價格輸入不符合規定,最多保留兩位小數");
}
}
else
{
System.out.println("權限不夠");
}
}
else if(code==5)
{
break;
}
}
}
if(level==0){
System.out.println("使用者名或密碼錯誤");
}
else{
System.out.println("成功退出");
}
scan.close();
}
//校驗登入,傳回權限值,0代表登入失敗
public static int check_person(String id,String pass){
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "select * from person where id='"+id+"' and password='"+pass+"'";
// id name author company price
ResultSet rs = stmt.executeQuery(sql);
int num=0;
int code=0;
while(rs.next())
{
code = rs.getInt(3);
num+=1;
}
if(num==0)
{
code=0;
}
rs.close();
stmt.close();
conn.close();
return code;
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
return 0;
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
return 0;
}
}
//利用正規表達式校驗價格,最多保留兩位小數
public static boolean check_price(String price){
Pattern price_check = Pattern.compile("\\d+\\.{0,1}\\d{0,2}");
return price_check.matcher(price).matches();
}
public static void insert_into(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
// id name author company price
sql = "insert into book values('"+args[0]+"','"+args[1]+"','"+args[2]+"','"+args[3]+"','"+args[4]+"')";
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(SQLException se){
System.out.println("插入失敗");
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
System.out.println("插入成功");
}
public static void select_from(String str) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql ="select * from book where id like '%"+str+"%' or name like '%"+str+"%' "
+ "or author like '%"+str+"%' or company like '%"+str+"%'";
ResultSet rs = stmt.executeQuery(sql);
int num=0;
System.out.println("書号\t書名\t作者\t出版社\t價格");
while(rs.next())
{
num+=1;
System.out.print(rs.getString(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getString(4)+"\t");
System.out.print(rs.getString(5)+"\n");
}
System.out.println("一共 "+num+" 條結果");
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
}
public static void delete_from(String str) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
/* 模糊删除
sql ="select from book where id like '%"+str+"%' or name like '%"+str+"%' "
+ "or author like '%"+str+"%' or company like '%"+str+"%'";
*/
sql = "delete from book where id = '"+str+"'";
stmt.executeUpdate(sql);
stmt.close();
conn.close();
System.out.print("删除成功\n");
}catch(SQLException se){
//Handle errors for JDBC
System.out.print("删除失敗\n");
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
}
public static void update(String id,String colname,String str) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "update book set "+colname+" = '"+str+"' where id = '"+id+"'";
stmt.executeUpdate(sql);
System.out.println("修改成功");
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
}
}
運作結果
person 表
圖書表
運作截圖
基本功能實作
以上僅為學習JDBC的一些記錄
僅供參考