标签:封装 ase connect cat getname this getc tac ssl
与数据库建立连接、发送 操作数据库的语句并处理结果。
3.PreparedStatement与Statement相比,具有什么优势?
1、相对比较安全,可以防止sql注入。
2、有预编译功能,相同操作批量数据效率较高。
3、使用PreparedStatement 接口,继承自 Statement接口比Statement对象使用起来更加灵活,更有效率
4. JDBC API主要功能:与数据库建立连接、执行SQL 语句、处理结果
DriverManager :依据数据库的不同,管理JDBC驱动
Connection :负责连接数据库并担任传送数据的任务
Statement :由 Connection 产生、负责执行SQL语句
ResultSet:负责保存Statement执行后所产生的查询结果
(1)定义记录的类(可选),这样做主要是为了便于操作和接口定义,是非必须的。
static class Student { private String Id; private String Name; private String Sex; private String Age; Student(String Name, String Sex, String Age) { this.Id = null; //default this.Name = Name; this.Sex = Sex; this.Age = Age; } public String getId() { return Id; } public void setId(String Id) { this.Id = Id; } public String getName() { return Name; } public void setName(String Name) { this.Name = Name; } public String getSex() { return Sex; } public void setSex(String Sex) { this.Sex = Sex; } public String getAge() { return Age; } public void setage(String Age) { this.Age = Age; } }
(2)连接的获取,在操作前必须先获取与数据库的连接。
private static Connection getConn() { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/samp_db"; String username = "root"; String password = ""; Connection conn = null; try { Class.forName(driver); //classLoader,加载对应驱动 conn = (Connection) DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; }
(3).象数据库当中插入元素
private static int insert(Student student) { Connection conn = getConn(); int i = 0; String sql = "insert into students (Name,Sex,Age) values(?,?,?)"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); pstmt.setString(1, student.getName()); pstmt.setString(2, student.getSex()); pstmt.setString(3, student.getAge()); i = pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; }
(4).修改数据库当中的元素
private static int update(Student student) { Connection conn = getConn(); int i = 0; String sql = "update students set Age=‘" + student.getAge() + "‘ where Name=‘" + student.getName() + "‘"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; }
(5).数据库中数据的查询
private static Integer getAll() { Connection conn = getConn(); String sql = "select * from students"; PreparedStatement pstmt; try { pstmt = (PreparedStatement)conn.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); int col = rs.getMetaData().getColumnCount(); System.out.println("============================"); while (rs.next()) { for (int i = 1; i <= col; i++) { System.out.print(rs.getString(i) + "\t"); if ((i == 2) && (rs.getString(i).length() < 8)) { System.out.print("\t"); } } System.out.println(""); } System.out.println("============================"); } catch (SQLException e) { e.printStackTrace(); } return null; }
(6).数据库中数据的删除
private static int delete(String name) { Connection conn = getConn(); int i = 0; String sql = "delete from students where Name=‘" + name + "‘"; PreparedStatement pstmt; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); i = pstmt.executeUpdate(); System.out.println("resutl: " + i); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return i; }
(7).测试代码是否正确
public static void main(String args[]) { JDBCOperation.getAll(); JDBCOperation.insert(new Student("Achilles", "Male", "14")); JDBCOperation.getAll(); JDBCOperation.update(new Student("Bean", "", "7")); JDBCOperation.delete("Achilles"); JDBCOperation.getAll(); }
Driver接口:表示Java驱动程序接口,所有具体的数据库厂商都要来实现此接口。
2.DriverManager类:驱动管理器类,用于管理所有注册的驱动程序。
3.Connection接口:表示Java程序和数据库的连接对象,拿到此接口,就表示和数据库已经建立连接
4.Statement接口:用于执行静态SQL语句并返回其结果
5.PreparedStatement接口:
6.ResultSet接口:用于封装查询出来的数据,表示结果集
标签:封装 ase connect cat getname this getc tac ssl
原文地址:http://www.cnblogs.com/houjiie/p/6177835.html