jdbc:java database connectivity
java
做用:
mysql
一、鏈接數據庫
web
二、向數據庫發送sql
sql
三、能夠對數據庫中的數據進行處理數據庫
java.sql.Driver 驅動的管理
工具
java.sql.Connection 鏈接的管理
學習
java.sql.Statement 操做數據庫管理
測試
java.sql.ResultSet 結果集
ui
一、jar包:
url
java project 須要新建lib文件,將jar放入其中,須要build path
web project jar 包 放入 webroot/web-inf/lib 便可,不須要build path
二、代碼(步驟能夠想像u盤的驅動)
第一步:加載驅動
第二步:建立鏈接
第三步:建立操做數據庫的對象
第四步:操做數據並返回結果
第五步:釋放資源
package com.itcast.base; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.Driver; public class Rumen { public static void main(String[] args) throws Exception { DriverManager.registerDriver(new Driver());//第一步:加載驅動 String url = "jdbc:mysql://localhost:3306/day06?useUnicode=true&characterEncoding=utf-8"; Connection conn = DriverManager.getConnection(url, "root", "root");//第二步:建立鏈接 Statement st = conn.createStatement();//第三步:建立操做數據庫的對象 String sql = "select * from user"; ResultSet rs = st.executeQuery(sql);//第四步:操做並返回結果 while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString(2); String password = rs.getString(3); String email = rs.getString(4); System.out.println(id+"---"+name+"---"+password+"---"+email); } rs.close(); //第五步:釋放資源 st.close(); conn.close(); } }
static(注意複習下靜態,又忘了) 只加載定義一次
(一)當類加載到內存中會執行靜態代碼塊中的代碼
(二)只執行一次
Class.forName("com.mysql.jdbc.Driver");
(1)解決代碼冗餘
(2)只註冊一次驅動
Connection conn = DriverManager.getConnection(url,user,password);
url: jdbc:mysql://localhost:3306/day06?useUnicode=true&characterEncoding=utf-8
jdbc:mysql://[ip地址]:[端口號]/[庫名]
本地: jdbc:mysql:///庫名
Statement sta = con.createStatement();
executeQuery(sql) //返回結果集
executeUpdate(sql) //返回受影響的條數
execute(sql) //寫select返回true,寫update,delete,insert返回false;
rs.getXX(String 字段的名字);
rs.getXX(int 索引值(從1開始));
沒有參數
沒有返回值
沒有static修飾
package com.itcast.jdbc; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; public class Utile { static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() throws Exception { Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql:///day06", "root", "root"); return conn; } public static void closeResource(ResultSet rs,Statement st,Connection con){ try { if(rs!=null){ rs.close(); } if(st!=null){ st.close(); } if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.itcast.jdbc; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class UtileTest { public static void main(String[] args) throws Exception { //add(); //delete(); //update(); select(); } private static void select() throws Exception { Connection con = Utile.getConnection(); Statement st = con.createStatement(); ResultSet rs=st.executeQuery("select * from user"); while(rs.next()){ int id = rs.getInt(1); //String name = rs.getString(2); String name = rs.getString("username"); String password = rs.getString(3); String email = rs.getString(4); System.out.println(id+"----"+name+"----"+password+"-----"+email); } Utile.closeResource(rs, st, con); } private static void update() throws Exception { Connection con = Utile.getConnection(); Statement st = con.createStatement(); int i = st.executeUpdate("update user set username='lisi' where id=3"); if(i!=0){ System.out.println("update success!"); }else{ System.out.println("update fail"); } } private static void delete() throws Exception { Connection con = Utile.getConnection(); Statement st = con.createStatement(); int i = st.executeUpdate("delete from user where id=4"); if(i!=0){ System.out.println("delete success"); }else{ System.out.println("delete fail"); } st.close(); con.close(); } private static void add() throws Exception { Connection con = Utile.getConnection(); Statement st = con.createStatement(); int i = st.executeUpdate("insert into user values(null,'liming','1234','liming@qq.com')"); if(i!= 0){ System.out.println("insert success!"); }else{ System.out.println("insert fail"); } st.close(); con.close(); } }
private static void select() throws Exception { Connection con = Utile.getConnection(); Statement st = con.createStatement(); Scanner sc = new Scanner(System.in); System.out.println("請輸入用戶名:"); String name = sc.nextLine(); System.out.println("請輸入密碼:"); String password = sc.nextLine(); String sql = "select * from user where username ='"+name+"'and password ='"+password+"'"; System.out.println(sql); ResultSet rs=st.executeQuery(sql); while(rs.next()){ int id = rs.getInt(1); //String name = rs.getString(2); String username = rs.getString("username"); String userpassword = rs.getString(3); String email = rs.getString(4); System.out.println(id+"----"+username+"----"+userpassword+"-----"+email); } Utile.closeResource(rs, st, con); }
數據所有能夠查詢出來了
PreparedStatement:表示預編譯的sql語句(預編譯完後,全部輸入的字符都不可以是關鍵字)
?:佔位符。(佔一個位置)
jdbc.properties區分大小寫
package com.itcast.jdbc; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; import java.util.ResourceBundle; public class Utile { private static final String driverName;//必須加final,不然可以經過反射獲得 private static final String url; private static final String user; private static final String password; static{ ResourceBundle bun = ResourceBundle.getBundle("jdbc"); driverName = bun.getString("driverName"); url = bun.getString("URL"); user = bun.getString("user"); password = bun.getString("password"); } static{ try { Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() throws Exception { Connection conn = (Connection) DriverManager.getConnection(url, user, password); return conn; } public static void closeResource(ResultSet rs,Statement st,Connection con){ try { if(rs!=null){ rs.close(); } if(st!=null){ st.close(); } if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.itcast.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class SqlTest { public static void main(String[] args) throws Exception { //add(); //delete(); update(); //select(); } private static void select() throws Exception { Connection con = Utile.getConnection(); String sql = "select * from user where username = ? and password = ?"; PreparedStatement st = con.prepareStatement(sql); st.setString(1, "tom"); st.setString(2, "123"); ResultSet rs=st.executeQuery(); while(rs.next()){ int id = rs.getInt(1); //String name = rs.getString(2); String username = rs.getString("username"); String userpassword = rs.getString(3); String email = rs.getString(4); System.out.println(id+"----"+username+"----"+userpassword+"-----"+email); } Utile.closeResource(rs, st, con); } private static void update() throws Exception { Connection con = Utile.getConnection(); String sql = "update user set username=? where id=?"; PreparedStatement st = con.prepareStatement(sql); st.setString(1, "lisisiw"); st.setString(2, "3");//這塊是int寫成String也能夠 int i = st.executeUpdate(); if(i!=0){ System.out.println("update success!"); }else{ System.out.println("update fail"); } } private static void delete() throws Exception { Connection con = Utile.getConnection(); String sql = "delete from user where id=?"; PreparedStatement st = con.prepareStatement(sql); st.setInt(1, 2); int i = st.executeUpdate(); if(i!=0){ System.out.println("delete success"); }else{ System.out.println("delete fail"); } Utile.closeResource(null, st, con); } private static void add() throws Exception { Connection con = Utile.getConnection(); String sql = "insert into user values(null,?,?,?)"; PreparedStatement st = con.prepareStatement(sql); st.setString(1, "lili"); st.setString(2, "wwwl"); st.setString(3, "lili@qq"); int i = st.executeUpdate(); if(i!= 0){ System.out.println("insert success!"); }else{ System.out.println("insert fail"); } Utile.closeResource(null, st, con); } }