操作前准备:下载mysql的驱动包,项目创建libs目录,将jar包拖进去,然后右击jar包build path然后add.
package com.fill.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class jdbcDemo {
public final static String URL="jdbc:mysql://127.0.0.1:3308/test";
public final static String PWD="root";
public final static String UNAME="root";
public final static void main(String[] args) {
// TODO Auto-generated method stub
//add();
//update();
//del();
find();
}
public static void add(){
try {
//1.用反射类加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection conn = (Connection) DriverManager.getConnection(URL, UNAME, PWD);
//3.构造sql语句
String sql="insert into admin_sql_inject(name,pwd)values('xiaobai','222')";
//4.构造statement实例(用来发送sql语句的载体)
Statement state = (Statement)conn.createStatement();
//5.执行sql语句(其实是发送到数据库)java里只有查询和更新两种,插入也属于更新
int result=state.executeUpdate(sql);
System.out.println(result);
//6.关闭连接:关闭发送和连接
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void add2(){
try {
//1.用反射类加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection conn = (Connection) DriverManager.getConnection(URL, UNAME, PWD);
//3.构造sql语句
//不建议直接用字符串拼接,不安全
String sql="insert into admin_sql_inject(name,pwd)values('xiaobai','222')";
//4.构造statement实例(用来发送sql语句的载体)
Statement state = (Statement)conn.createStatement();
//5.执行sql语句(其实是发送到数据库)java里只有查询和更新两种,插入也属于更新
int result=state.executeUpdate(sql);
System.out.println(result);
//6.关闭连接:关闭发送和连接
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void find(){
//查询要用到 ResultSet接口
try {
//1.用反射类加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection conn = (Connection) DriverManager.getConnection(URL, UNAME, PWD);
//3.构造sql语句
String sql="select * from admin_sql_inject where 1";
//4.构造statement实例(用来发送sql语句的载体)
Statement state = (Statement)conn.createStatement();
//5.执行sql语句(其实是发送到数据库)java里只有查询和更新两种,插入也属于更新
ResultSet rs=state.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getInt("id")+"="+rs.getString("name")+"="+rs.getString("pwd"));
}
//6.关闭连接:关闭发送和连接
rs.close();
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void del(){
try {
//1.用反射类加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection conn = (Connection) DriverManager.getConnection(URL, UNAME, PWD);
//3.构造sql语句
String sql="delete from admin_sql_inject where name='shuishui1'";
//4.构造statement实例(用来发送sql语句的载体)
Statement state = (Statement)conn.createStatement();
//5.执行sql语句(其实是发送到数据库)java里只有查询和更新两种,插入也属于更新
int result=state.executeUpdate(sql);
System.out.println(result);
//6.关闭连接:关闭发送和连接
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void update(){
try {
//1.用反射类加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接对象
Connection conn = (Connection) DriverManager.getConnection(URL, UNAME, PWD);
//3.构造sql语句
String sql="update admin_sql_inject set name='shuishui1' where name='shuishui'";
//4.构造statement实例(用来发送sql语句的载体)
Statement state = (Statement)conn.createStatement();
//5.执行sql语句(其实是发送到数据库)java里只有查询和更新两种,插入也属于更新
int result=state.executeUpdate(sql);
System.out.println(result);
//6.关闭连接:关闭发送和连接
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}