Java操作mysql資料庫簡單例子

來源:互聯網
上載者:User

標籤:java操作mysql例子

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"; //串連資料庫的URL地址

public static String username = "root"; //資料庫的使用者名稱

public static String password = "";  //資料庫的密碼

public static Connection conn=null;//連線物件

    public static Statement stmt=null;//語句

    public static ResultSet rs = null;//結果集

//1.載入MySQL資料庫驅動

    static

    {

try {

Class.forName("com.mysql.jdbc.Driver");

//2、建立資料庫連接

conn = (Connection) DriverManager.getConnection(url,username,password);

 

if(conn != null)

{

System.out.println("資料庫連接正常");

}

else

{

System.out.println("資料庫連接失敗");

}

catch (Exception e)

{

e.printStackTrace();

}

  }


  //查詢所有的學生資料

  public static void query()

  {

  String sql = "select * from students;";

  try {

  stmt = conn.createStatement();

  rs = stmt.executeQuery(sql);

  while(rs.next())

  {

  System.out.println("學號:"+rs.getInt("sid")+",姓名:"+rs.getString("sname")+",年齡:"+rs.getInt("age")+",性別:"+rs.getString("gender"));

  }

 

} catch (Exception e)

  {

e.printStackTrace();

  }

  finally 

  {

destoryResource(); 

}

  }

    


//新增學生方法

public static boolean add()

{

String sql = "insert into Students values (11,‘張三天‘,138,‘f‘,‘[email protected]‘,‘廣州陽江‘);";

try

{

stmt = conn.createStatement();

int result = stmt.executeUpdate(sql);

if(result > 0)

{

System.out.println("資料添加成功");

return true;

}

else

{

System.out.println("資料庫添加失敗");

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();

}

  }

 

  //刪除指定學號的學生資料

  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("資料添刪除成功");

return true;

}

else

{

System.out.println("資料添沒有刪除");

return false;

}

}

catch(Exception ex)

{

ex.printStackTrace();

return false;

}

finally

{

destoryResource();

}

  }

 

  //修改所有學生的年齡為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("------刪除學號為11的學生之後--------");

jdbc02.query();

jdbc02.update(20);

System.out.println("------修改所有學生年齡為20歲--------");

jdbc02.query();

jdbc02.destoryallResource();  //釋放資源

  }

}


本文出自 “知止內明” 部落格,請務必保留此出處http://357712148.blog.51cto.com/6440370/1894631

Java操作mysql資料庫簡單例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.