JDBC的含义
JDBC(Java DataBase Connectivity)Java数据库连接技术的简称,提供连接各种常用数据库的能力
JDBC的初级使用
只导入ojdbc.jar
使用properties文件和工具类DB.class(此类自建的公用的类)
在项目下的src目录下,创建abc.properties文件,并输入
1 2 3 4
| a=oracle.jdbc.driver.OracleDriver b=jdbc:oracle:thin:@localhost:1521:orcl c=raoweijia d=123
|
在项目下的src目录下,创建tools目录,然后在此目录下创建DB.class(DBUtils的三大功能之一),并输入
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package tools;
import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties;
public class DB { public Connection conn = null; public PreparedStatement ps = null; public ResultSet rs = null;
static String driver = null; static String url = null; static String name = null; static String pwd = null;
static { InputStream is = DB.class.getClassLoader().getResourceAsStream("abc.properties"); Properties pp = new Properties(); try { pp.load(is); driver = pp.getProperty("a"); url = pp.getProperty("b"); name = pp.getProperty("c"); pwd = pp.getProperty("d"); } catch (IOException e) { e.printStackTrace(); } }
public void getConn() { try { Class.forName(driver); conn = DriverManager.getConnection(url, name, pwd); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } }
public void close() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
|
在项目下的src目录下,创建test目录,然后在此目录下创建Test.class继承DB.class,并输入
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package test;
import tools.DB; import java.sql.ResultSet; import java.sql.SQLException;
public class Test extends DB {
public int insert() { String sql= "INSERT INTO STUDENT (ID,NAME,AGE,GRADE) VALUES ('1001','raoweijia',9,'4') "; try { getConn(); ps = conn.prepareStatement(sql); int a = ps.executeUpdate(); return a; } catch (SQLException e) { e.printStackTrace(); }finally { close(); } return -1; }
public boolean delete() { String sql= "DELETE FROM STUDENT WHERE NAME='CAOYANG' "; try { getConn(); ps = conn.prepareStatement(sql); boolean a = ps.execute(); return a; } catch (SQLException e) { e.printStackTrace(); }finally { close(); } return false; }
public void find() { String sql= "select * FROM STUDENT WHERE NAME=? "; try { getConn(); ps = conn.prepareStatement(sql); ps.setString(1,"caohaha"); ResultSet a = ps.executeQuery(); while(a.next()){ System.out.println("我的名字是"+a.getString("name")+",今年"+a.getInt("age")+"岁,小学"+a.getString("grade")+"年级"); } } catch (SQLException e) { e.printStackTrace(); }finally { close(); }
}
public int update() { String sql= "update STUDENT set name='caohaha' WHERE NAME='CAOYANG' "; try { getConn(); ps = conn.prepareStatement(sql); int a = ps.executeUpdate(); return a; } catch (SQLException e) { e.printStackTrace(); }finally { close(); } return -1; }
public static void main(String[] args) { Test test = new Test();
int a = test.update(); System.out.print(a); } }
|
使用Properties文件,是为了防止变量设置修改后需重新编写代码,所以把变量设置写在文件中,让使用它时读取它.properties文件使用规则如下
Properties类读取配置文件的常用方法
PreparedStatement避免Statement发生的一些错误,所以要使用PreparedStatement,具体优点如下
PreparedStatement继承Statement,可以使用Statement的常用方法,常用方法如下
查询结果集ResultSet的常用方法
JDBC总结