久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Java操作mysql數(shù)據(jù)庫(kù)實(shí)例介紹

共計(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í)用的文章!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-08-01發(fā)表,共計(jì)3061字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒有評(píng)論)
主站蜘蛛池模板: 云梦县| 喀喇| 齐齐哈尔市| 绵竹市| 怀宁县| 新乡县| 栾川县| 青阳县| 图木舒克市| 铁力市| 贵州省| 离岛区| 华容县| 南阳市| 阿拉善右旗| 潼关县| 建瓯市| 汶川县| 长沙县| 石景山区| 荣昌县| 高雄市| 容城县| 疏附县| 武强县| 金湖县| 昌乐县| 开江县| 容城县| 丹凤县| 英超| 泾川县| 迭部县| 河北省| 江津市| 绥滨县| 赤水市| 沈丘县| 庐江县| 塘沽区| 漳州市|