1. 程式人生 > >在java中對數據庫進行增刪改查(轉)

在java中對數據庫進行增刪改查(轉)

ima ive upd line 增加 key get cat imp

1.java連接MySql數據庫

技術分享圖片

代碼區域:

技術分享圖片

技術分享圖片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.oracle.jdbc.demo1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JDBCDemo {
//四個屬性(四個常量的字符串) /* jdbcName url user password */ private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password=
"123456"; /* * 一個類(DriverManeger)四個接口(Connection、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password);
//獲得conn就表示獲取了數據庫的連接 System.out.println("連接數據庫成功"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

  

2.在java中向數據庫添加數據

第一種方法:添加數據

技術分享圖片

代碼區域:

技術分享圖片 技術分享圖片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package com.oracle.jdbc.demo2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class JDBCAdd { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一個類(DriverManeger)四個接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //增加數據的操作 String name="田雨"; String sex="女"; String sql="insert into person values(null,‘"+name+"‘,‘"+sex+"‘)"; PreparedStatement pst=conn.prepareStatement(sql); //準備執行sql語句 int i=pst.executeUpdate(); //返回成功插入數據的行數 System.out.println("成功添加了"+i+"條記錄"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

  

第二中方法:添加數據

代碼區域:

技術分享圖片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.oracle.jdbc.demo2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class JDBCAdd2 { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一個類(DriverManeger)四個接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //增加數據的操作 String name="田雨2"; String sex="女"; String sql="insert into person values(null,?,?)"; PreparedStatement pst=conn.prepareStatement(sql); //準備執行sql語句 pst.setString(1, name); //填充第1個問好 pst.setString(2, sex); //填充第2個問好 int i=pst.executeUpdate(); //返回成功插入數據的行數 System.out.println("成功添加了"+i+"條記錄"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

  

3.在java中修改數據庫的內容

技術分享圖片

代碼區域:

技術分享圖片

技術分享圖片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package com.oracle.jdbc.demo3; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class JDBCModify { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一個類(DriverManeger)四個接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //修改數據的操作 int id=2; String name="王希寶"; String sex="男"; String sql="update person set name=?,sex=? where id=?"; PreparedStatement pst=conn.prepareStatement(sql); //準備執行sql語句 pst.setString(1, name); //填充第1個問好 pst.setString(2, sex); //填充第2個問好 pst.setInt(3, id); int i=pst.executeUpdate(); //返回成功修改數據的行數 System.out.println("成功修改了"+i+"條記錄"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

  

4.在java中刪除數據庫的內容

技術分享圖片

代碼區域:

技術分享圖片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package com.oracle.jdbc.demo4; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class JDBCDel { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一個類(DriverManeger)四個接口(Connection、PreparedStatement、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //刪除數據的操作 int id=2; String sql="delete from person where id=?"; PreparedStatement pst=conn.prepareStatement(sql); //準備執行sql語句 pst.setInt(1, id); int i=pst.executeUpdate(); //返回成功刪除數據的行數 System.out.println("成功刪除了"+i+"條記錄"); } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

  

5.在java中查看數據庫的內容

技術分享圖片

代碼區域:

技術分享圖片

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.oracle.jdbc.demo5; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCFindAll { private static final String jdbcName="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://127.0.0.1:3306/emp_dept"; private static final String user="root"; private static final String password="123456"; /* * 一個類(DriverManeger)四個接口(Connection、PreparedStatement、ResultSet、) * */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn=null; try { Class.forName(jdbcName); conn=DriverManager.getConnection(url, user, password); //查詢數據的操作 String sql="select id,name,sex from person"; PreparedStatement pst=conn.prepareStatement(sql); //準備執行sql語句 ResultSet rs=pst.executeQuery(); while(rs.next()){ int id=rs.getInt("id"); String name=rs.getString("name"); String sex=rs.getString("sex"); System.out.println(id+" "+name+" "+sex); } } catch (Exception e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

  

在java中對數據庫進行增刪改查(轉)