標籤: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資料庫簡單例子