eclipse连接MySQL
1.下载jar包
网址:https://mvnrepository.com
点击网址进去后,可以直接搜索MySQL,选择这个
点击进入,选择版本就可以下载了
我选择的是这个,一般选择使用人多的包,点击jar就可以下载。
连接前要让数据库和eclipse的编码保持一致。
在要使用的工程下新建一个lib包,然后将包复制粘贴过来 ,但是复制过来还不可以操作,要进行下面的一步。
eclipse连接数据库
扩展问题 JVM什么时候将类加载到内存中?
答:在第一次主动使用类的时候加载,且只加载一次,并不是一开始就把所有的类加载内存
插入操作
Connection conn = null;
try {
/**1.加载MySQL的驱动类 com.mysql.jdbc.Driver*/
//将类加载到内存
Class.forName("com.mysql.jdbc.Driver");//JDK1.7及以下必须JDK1.8及以上不是必需
/**2.获取数据库连接对象*/
// 连接数据库需要哪些要素
// host(地址) port(端口) user(用户) password(密码)
String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8"; //url jdbc有自己的写法 jdbc:oracl
String user="root";
String password="123456";
conn = DriverManager.getConnection(url, user, password);
/**3.实现数据库的操作*/
// 3.1 编写sql语句
String insert_sql = "insert into commoditytype (ct_id,ct_name) values (8,'电脑配件')";
// 3.2 获取执行对象
Statement state = conn.createStatement();
//获取执行state的返回值
int s = state.executeUpdate(insert_sql);
System.out.println(s);
}catch(Exception e) {
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //关闭数据库连接对象
}
修改操作
以下代码只更改以上代码的 实现数据库的操作部分
// 3.1 编写sql语句
String update_sql = "update commoditytype set ct_name='电脑' where ct_id=8";
// 3.2 获取执行对象
Statement state = conn.createStatement();
//获取执行state的返回值
int s = state.executeUpdate(insert_sql);
System.out.println(s);
删除操作
// 3.1 编写sql语句
String delect_sql = "delete from commoditytype where ct_id=8";
// 3.2 获取执行对象
Statement state = conn.createStatement();
//获取执行state的返回值
int s = state.executeUpdate(insert_sql);
System.out.println(s);
查询操作
// 3.1 编写sql语句
String query_sql = "select * from customer";
// 3.2 获取执行对象
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(query_sql);
while(rs.next()) {
//迭代数据集中的每一行,从每一行中获取相应的数据
//getString 有两种重载方法,通过属性或属性的排列次序
// String cu_id = rs.getString(1);
// String cu_name = rs.getString(2);
// String cu_phone = rs.getString(3);
// int cu_gender = rs.getInt(4);
// String cu_address = rs.getString(5);
String cu_id = rs.getString("cu_id");
String cu_name = rs.getString("cu_name");
String cu_phone = rs.getString("cu_phone");
int cu_gender = rs.getInt("cu_gender");
String cu_address = rs.getString("cu_address");
System.out.println(cu_id+"\t:\t"+cu_name+"\t|\t"+cu_phone+"\t|\t"+cu_gender+"\t|\t"+cu_address);
}
模拟登录功能
/**
* 我们来模拟登录功能
*/
public class Demo02 {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8"; //url jdbc有自己的写法 jdbc:oracl
String user="root";
String password="123456";
conn = DriverManager.getConnection(url, user, password);//jdbc的数据库驱动类DriverManager.getConnection(),返回值为Connection
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名称:");
System.out.println("请输入用户手机号:");
String sql = "select count(*) from customer where cu_name='"+cu_name+"' and cu_phone='"+cu_phone+"'";
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql);
rs.next();
int count=rs.getInt(1);
if(count>0) {
System.out.println("登录成功!");
}else {
System.out.println("用户名或手机号错误,登录失败!");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
上面的代码中实现 sql注入攻击,只要输入姓名为"’ or 1=1 #",密码无所谓是会什么。
String cu_name="' or 1=1 #";
String cu_phone="dadadadad";
String cu_name="’ or 1=1 #"; 有"or" 且1=1是永远成立的 , 并且密码被注释掉了,所以能够不用密码登录
解决注入攻击
- 禁止前端登录界面禁止输入"#";
- 在后端实现查询 来解决注入攻击
如何实现查询 来解决注入攻击
利用PreparedStatement类的prepareStatement方法实现
使用PreparedStatement的好处:
1). 提高代码的可读性和可维护性;
2). 最大程度的提高性能:
3). 可以防止SQL注入
以下是代码实现
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8"; //url jdbc有自己的写法 jdbc:oracl
String user="root";
String password="123456";
conn = DriverManager.getConnection(url, user, password);
// String cu_name="' or 1=1 #"; // String cu_phone="dadadadad";
String cu_name="刘德华"; String cu_phone="7777";
String sql = "select count(*) from customer where cu_name=? and cu_phone=?";
PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, cu_name);
ps.setString(2, cu_phone);
ResultSet rs = ps.executeQuery(); rs.next();
System.out.println(rs.getInt(1));
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, cu_phone);
ps.setString(2, cu_id);
int line = ps.executeUpdate();
System.out.println(line);
}catch(Exception e) {
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}