Java是一種面向對象的語言
||語言不一樣,須要交流,找個翻譯(java只出標準,數據庫實現)
Mysql是結構化查詢語言java
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,user,password);
須要導包:import.java.sql.Connection;
import java.sql.DriverManager;
String url = "jdbc:mysql://IP:3306/數據庫名";
String user = "root";
String password = "數據庫密碼";
Statement st = conn.createStatement();
須要導包 import java.sql.Statement;
int row = st.executeUpdate(sql語句);
row返回值表示影響的行數練習:CRUD,使用方法而且關閉資源mysql
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class CRUD { public static void main(String[] args) { //add("上海"); //delete(4); //update("南京",4); selectall(); } public static void add(String cityname) { Connection conn = null; Statement st = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456"); st = conn.createStatement(); int row = st.executeUpdate("insert into city(cityname) values ('"+cityname+"')"); System.out.println("受影響的行數爲:"+row); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void delete(int cityid) { Connection conn = null; Statement st = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456"); st = conn.createStatement(); int row = st.executeUpdate("delete from city where cityID = "+cityid); System.out.println("受影響的行數爲:"+row); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void update(String cityname,int cityid) { Connection conn = null; Statement st = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456"); st = conn.createStatement(); int row = st.executeUpdate("update city set cityname = '"+cityname+"' where cityid = "+cityid); System.out.println("受影響的行數爲:"+row); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void selectall() { Connection conn = null; Statement st = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456"); st = conn.createStatement(); rs = st.executeQuery("select * from city"); while(rs.next()) { int cityid = rs.getInt("cityid"); String cityname = rs.getString("cityname"); System.out.println(cityid+"======>"+cityname); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
一張表對應一個操做類(crud),操做類中包含的是CRUD
例如:對emp表操做,那麼java中對應的類就是EmpDao(Data Access Object) 這個類儘可能放到dao包下
包名:企業域名反向寫 www.baidu.com====>com.baidu + 開發人員名稱縮寫 ====> com.baidu.wlq.daosql
練習:抽取一個BaseDao父類(7個屬性,一個方法)
寫一個員工表的操做類EmpDao 繼承Base Dao。包含數據庫
並寫一個測試類來測試上面5個方法測試
package com.alibaba.wlq.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class BaseDao { protected Connection conn = null; protected Statement st = null; protected ResultSet rs = null; protected String driver = "com.mysql.jdbc.Driver"; protected String url = "jdbc:mysql://localhost:3306/emp"; protected String user = "root"; protected String password = "123456"; public void closeAll() { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if(st != null) { try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
package com.alibaba.wlq.dao; import java.sql.DriverManager; import java.sql.SQLException; public class EmpDao extends BaseDao{ /** * 增長員工信息 * @param ename 員工姓名 * @param sal 員工薪水 * @param job 員工職位 */ public void add(String ename,Double sal,String job) { try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); st = <u>conn</u>.createStatement(); String sql = "insert into emp(ename,sal,job,hiredate) values ('"+ename+"',"+sal+",'"+job+"',now())"; int row = st.executeUpdate(sql); System.out.println("影響了"+row+"行"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } /** * 根據員工編號刪除員工的信息 * @param empno 員工編號 */ public void delete(int empno) { try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); st = conn.createStatement(); String sql = "delete from emp where empno ="+empno; int row = st.executeUpdate(sql); System.out.println("影響了"+row+"行"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } /** * 根據員工編號查詢員工信息 * @param empno 員工編號 */ public void select(int empno) { try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); st = conn.createStatement(); String sql = "select * from emp where empno = " + empno; rs = st.executeQuery(sql); while(rs.next()) { System.out.println("員工編號:"+rs.getInt("empno")); System.out.println("員工姓名:"+rs.getString("ename")); System.out.println("員工薪資:"+rs.getDouble("sal")); System.out.println("員工入職時間:"+rs.getDate("hiredate")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { closeAll(); } } /** * 根據員工編號修改員工姓名、薪資、職位 * @param ename 員工姓名 * @param sal 薪資 * @param job 職位 * @param empno 員工編號 */ public void update(String ename,Double sal,String job,int empno) { try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); st = conn.createStatement(); String sql = "update emp set ename = '"+ename+"',sal = '"+sal+"',job='"+job+"' where empno = "+empno; int row = st.executeUpdate(sql); System.out.println("影響了"+row+"行"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { closeAll(); } } /** * 查詢全部員工的信息 * @param empno員工編號 */ public void selectALL() { try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); st = conn.createStatement(); String sql = "select * from emp"; rs = st.executeQuery(sql); while(rs.next()) { System.out.print("員工編號:"+rs.getInt("empno")+"\t"); System.out.print("員工姓名:"+rs.getString("ename")+"\t"); System.out.print("員工領導編號:"+rs.getInt("mgr")+"\t"); System.out.print("員工薪資:"+rs.getDouble("sal")+"\t"); System.out.println("員工入職時間:"+rs.getDate("hiredate")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { closeAll(); } } }
package com.alibaba.wlq.dao; public class Test1 { public static void main(String[] args) { EmpDao emp = new EmpDao(); //emp.add("劉德華",20000.0,"演員"); //emp.delete(1016); //emp.select(1001); //emp.update("劉德華", 20000.0, "演員", 1015); emp.selectALL(); } }