@martin0207
2018-04-14T01:12:39.000000Z
字数 2755
阅读 808
Java学习
学习Java的路上,一步步走到数据库这里,当然选择了大多数人会选择的MySql数据库。
安装教程
教程里已经写的非常详细,就不再赘述。
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序
安装与解压版的MySql并没有JDBC的jar包,需要在官网重新下载。
官网首页 → downloads → MYSQL Connectors(左侧选择列表) → Connector/J
使用-bin文件
public static void query() {
/*
* 1.创建Connection 需要指定相应的连接字符串,用户名和密码
*/
String url = "jdbc:mysql://localhost:3306/db_test?useSSL=true";
String user = "test";
String password = "123456";
Connection connection = null;
Statement statement = null;
ResultSet set = null;
try {
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
/**
* 2.创建sql语句 执行SQL语句,需要使用Statement
*/
String sql = "select * from user";
/**
* 增加或更新数据时,使用 excuteUpdate(sql);
*/
//sql = "insert into user (name) value ('插入用户')";
//sql = "update user set pwd='150', address='adress' where id=6";
//statement.executeUpdate(sql);
/**
* 在获取的 statement之后, 如果执行更新,直接使用state.executeUpdate(sql);
* 如果是执行查询,需要通过state.executeQuery(sql)完成,返回是ResultSet记录集
*/
set = statement.executeQuery(sql);
/**
* 遍历记录集的方式
*/
while (set.next()) {
System.out.println(set.getInt("id") + "," + set.getString("name"));
/**
* 也可以用数据库的投影顺序来访问字段,但是不建议,顺序可能会变
* 一般都是使用字段名字访问
*/
System.out.println(set.getInt(1)+"," + set.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
/**
* 操作完成之后,需要释放连接 先释放 ResultSet 再释放 Statement 最后释放 Connection 倒叙释放
*/
try {
if (set != null)
set.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如上即正常使用JDBC。
useSSL=true 如果不添加,IDEA编辑器会提示错误,但是不影响运行。
Statement 使用时,存在安全问题:
登录语句:select count() from user where name='name' and pwd='123';
用户名输入 admin"';// 时,查询语句为:select count() from user where name='admin"';// and pwd='123';
这就尴尬了,系统认为,查询语句是select count(*) from user where name='admin"';如果系统内有admin这个账号时,肯定登录成功—— SQL注入攻击
所以,我们在使用时,并不用Statement,而是使用PreparedStatement:
public static void insert2() {
String url = "jdbc:mysql://localhost:3306/db_test?useSSL=true";
String user = "test";
String password = "123456";
String sql = "";
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DriverManager.getConnection(url, user, password);
sql = "insert into user (name) value ('query2')";
// sql = "update user set pwd='150', address='adress' where id=6";
statement = connection.prepareStatement(sql);
statement.executeUpdate();
/*
// 可以使用PreparedStatement 代替Statement,这样可以有效的解决SQL注入攻击的问题
PreparedStatement pStatement = connection.prepareStatement(sql);
pStatement.setInt(1, 5);
set = pStatement.executeQuery();
*/
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (connection != null)
connection.close();
if (statement != null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}