jdbc使用总结
连接数据库方式
1.public String url="jdbc:mysql://localhost:3306/day17";
public String user="root";
public String password="root";
public void test1(){
Driver driver=new com.mysql.jdbc.Driver();
Properties props=new Properties();
props.setProperty("user",user);
props.setProperty("password",password);
Connection conn=driver.connect(url,props);
System.out.println(conn);
}
2.第二种方式连接数据库
public void test2(){
Driver driver=new Driver(url);
DriverManager.registerManager(driver);
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
3.第三种方式
public void test3(){
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
jdbc接口核心api
Driver接口:表示java驱动程序接口,所有的具体的数据库厂商要求来实现此接口。
方法:
connect(url,properties):连接数据库的方法
其中url表示连接数据库的url,
url语法:jdbc协议,数据库子协议://主机:端口/数据库
user:数据库的用户名
password:数据库用户密码
DriverManager类:驱动管理器类,用于管理所有注册的驱动程序
registerDriver(driver):注册驱动类对象
Connection getConnection(url,user,password);//获取连接对象
Connection接口:表示java程序和数据库的连接对象。
Statement createStatement():创建Statement对象
PreparedStatement prepareStatement(String sql) 创建PreparedStatement对象
CallableStatement prepareCall(String sql) 创建CallableStatement对象
Statement接口:用于执行静态的sql语句
int executeUpdate(String sql):执行静态的更行sql语句(DDL,DML)
ResultSet executeQuery(String sql):执行的静态的查询sql语句(DQL)
|-PreparedStatement接口:用于执行预编译sql语句
|- int executeUpdate() : 执行预编译的更新sql语句(DDL,DML)
|-ResultSet executeQuery() : 执行预编译的查询sql语句(DQL)
|-CallableStatement接口:用于执行存储过程的sql语句(call xxx)
|-ResultSet executeQuery() : 调用存储过程的方法
ResultSet接口:用于封装查询出来的数据
boolean next():将光标移动到下一行
getXX():获取列的值
执行数据库定义语言
Statement stmt=null;
Connection conn=null;
public void test3(){
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
String sql="create table A(id int)"
int count=stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行!");
}
执行DML语句
public class Demo2{
private String url="jdbc:mysql://localhost:3306/day17"
}
执行插入数据
public void testInsert(){
Connection conn=null;
Statement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.createStatement();
String sql="insert into student(name,gender) values('掌声你','女')"
int count=stmt.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}finally{
jdbcUtil.close();
}
}
更新操作
Connection conn=null;
Statement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.createStatement();
String sql="update student set name='"+name+"'where id=1";
int count=stmt.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close();
}
删除操作
Connection conn=null;
Statement stmt=null;
try{
conn=JdbcUtil.getConnection();
state=conn.createStatement();
String sql="delete student where id=1";
int count=conn.executeUpdate(sql);
System.out.println("影响了"+count+"行");
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close();
}
查询
Connction conn=null;
Statement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.createStatement();
String sql="select * from student";
ResultSet rs=stmt.executeUpdate(sql);
while(rs.next()){
int id=rs.getInt("id");
String name=rs.getString("name");
String gender=rs.getString("gender");
System.out.println("id"+id+",name"+name+",gender"+gender);
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close();
}
}
使用PreparedStatement执行sql语句
添加
public void test3(){
Conncetion conn=null;
PrepareStatement pre=null;
try{
conn=JdbcUtil.getConnction();
String sql="insert student(name,gender) values(?,?)"
pre=conn.prepareStatement(sql);
pre.setString(1,"张三");
pre.setString(2,"男");
int count=pre.executeUpdate();
System.out.println("影响了"+count+"行");
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}finally{
JdbcUtil.close(conn,stmt);
}
}
更新
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
stmt=conn.prepareStatement();
String sql="update student set name=? where id=?";
stmt.setString(1,"张三");
stmt.setInt(2,7);
stmt.executeUpdate(sql);
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(conn,stmt);
}
删除
public void testDelete(){
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnction();
String sql="delete student where id=?";
stmt=conn.prepareStatement(sql);
stmt.setInt(1,2);
int count=stmt.executeUpdate();
System.out.println("影响了"+count+"行");
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close();
}
}
查询
public void testQuery(){
Connection conn=null;
PreparedStatement stmt=null;
try{
conn=JdbcUtil.getConnection();
String sql="select * from student";
stmt=conn.prepareStatement(sql);
int count=stmt.executeUpdate();
System.out.println("影响了"+count+"行")
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtil.close(conn,stmt);
}
}
PreparedStatement 与Statement的不同
1.语法不同:PreparedStatement可以使用预编译的sql,而Statement只能使用静态的sql
2.效率不同:PreparedStatement可以使用sql缓存区,效率比Statement高
3.安全性不同:PreparedStatement可以有效防止sql注入,而Statement不能防止sql注入