共計(jì) 3061 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
這篇文章主要介紹“Java 操作 mysql 數(shù)據(jù)庫(kù)實(shí)例介紹”,在日常操作中,相信很多人在 Java 操作 mysql 數(shù)據(jù)庫(kù)實(shí)例介紹問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java 操作 mysql 數(shù)據(jù)庫(kù)實(shí)例介紹”的疑惑有所幫助!接下來,請(qǐng)跟著丸趣 TV 小編一起來學(xué)習(xí)吧!
package com.Jdbc.demo;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.jdbc.Connection;
public class jdbc02 {
public static final String url = jdbc:mysql://localhost:3306/school?useUnicode=true characterEncoding=UTF-8 // 連接數(shù)據(jù)庫(kù)的 URL 地址
public static String username = root // 數(shù)據(jù)庫(kù)的用戶名
public static String password = // 數(shù)據(jù)庫(kù)的密碼
public static Connection conn=null;// 連接對(duì)象
public static Statement stmt=null;// 語句
public static ResultSet rs = null;// 結(jié)果集
//1. 加載 MySQL 數(shù)據(jù)庫(kù)驅(qū)動(dòng)
static
{
try {
Class.forName(com.mysql.jdbc.Driver
//2、建立數(shù)據(jù)庫(kù)連接
conn = (Connection) DriverManager.getConnection(url,username,password);
if(conn != null)
{
System.out.println(數(shù)據(jù)庫(kù)連接正常
}
else
{
System.out.println(數(shù)據(jù)庫(kù)連接失敗
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
// 查詢所有的學(xué)生資料
public static void query()
{
String sql = select * from students;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(學(xué)號(hào):+rs.getInt( sid)+ , 姓名:+rs.getString(sname)+ , 年齡: +rs.getInt(age)+ , 性別:+rs.getString(gender));
}
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
destoryResource();
}
}
// 添加學(xué)生方法
public static boolean add()
{
String sql = insert into Students values (11, 張三天 ,138, f , zhangsan@qq.com , 廣州陽(yáng)江
try
{
stmt = conn.createStatement();
int result = stmt.executeUpdate(sql);
if(result 0)
{
System.out.println(數(shù)據(jù)添加成功
return true;
}
else
{
System.out.println(數(shù)據(jù)庫(kù)添加失敗
return false;
}
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
finally
{
destoryResource();
}
}
// 釋放資源的方法
public static void destoryResource()
{
try {
if(rs != null)
{
rs.close();
rs = null;
}
if (stmt != null)
{
stmt.close();
stmt = null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
// 釋放最后資源
public static void destoryallResource()
{
try
{
if (conn != null)
{
conn.close();
conn = null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
// 刪除指定學(xué)號(hào)的學(xué)生資料
public static boolean delete(int sid)
{
String sql = delete from students where sid= +sid;
try
{
stmt = conn.createStatement();
int result = stmt.executeUpdate(sql);
if(result 0)
{
System.out.println(數(shù)據(jù)添刪除成功
return true;
}
else
{
System.out.println(數(shù)據(jù)添沒有刪除
return false;
}
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
finally
{
destoryResource();
}
}
// 修改所有學(xué)生的年齡為 20 歲
public static boolean update(int age)
{
String sql = update students set age= +age;
try
{
stmt = conn.createStatement();
int result = stmt.executeUpdate(sql);
if(result 0)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
finally
{
destoryResource();
}
}
public static void main(String[] args)
{
jdbc02.query(); // 查詢語句
if (jdbc02.add())
{
System.out.println(添加成功!
}
else
{
System.out.println(添加失敗!
}
System.out.println(———————
jdbc02.query();
jdbc02.delete(11);
System.out.println(—— 刪除學(xué)號(hào)為 11 的學(xué)生之后 ——–
jdbc02.query();
jdbc02.update(20);
System.out.println(—— 修改所有學(xué)生年齡為 20 歲 ——–
jdbc02.query();
jdbc02.destoryallResource(); // 釋放資源
}
}
到此,關(guān)于“Java 操作 mysql 數(shù)據(jù)庫(kù)實(shí)例介紹”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!