MYSQL中的JDBC程序:
第一种方式:
- 先加入mysql-connector-java-8.0.27.jar包
- MySQL5之后,启动驱动这一步骤可省略,mysql-connector-java-8.0.27.jar包会自动启动的
- url一般是:“jdbc:mysql://127.0.0.1:3306/数据库名称”
标准是:“jdbc:mysql://ip地址(域名):端口号/数据库名称?参数键值对1&参数键值对2…”
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//启动驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//用户连接信息
String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username="root";
String password="123456";
//连接成功后,第一步获得connection(代表数据库),第二步再获得能够执行sql对象的statement
Connection connection=DriverManager.getConnection(url,username,password);
Statement statement= connection.createStatement();
//statement,可以开始执行SQL的对象了
String sql="select * from product";
//执行完sql语句并且返回一个结果集
ResultSet resultSet=statement.executeQuery(sql);
//result.next()返回的是boolean型
while(resultSet.next()){
//调用方法,给其方法传入字段名
//来显示刚才查询到的内容
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("price"));
}
//关闭资源
connection.close();
statement.close();
resultSet.close();
}
}
第二种方法:
因为上面的用户连接信息,加载驱动,获得Connection,关闭资源都是一样的,所以为了降低耦合性,可以将用户连接信息放在一个properties文件中,将加载驱动,获得Connection,关闭资源都放在一个工具类中,用户调用的时候直接调用工具类即可
首先先创建一个 . properties文件,在其中放用户连接信息:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=123456
工具类:
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class jdbctool{
private static String driver=null;
private static String url=null;
private static String username=null;
private static String password=null;
static {
try{
//从工具db.properties中获得数据库的信息
InputStream in=jdbctool.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties=new Properties();
properties.load(in);
//因为和流有关,暂时不理解,先去学流,之前学的忘完了
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
//加载驱动
Class.forName(driver);
}
catch (Exception e) {
e.printStackTrace();
}
}
//创建获得数据库Connection的方法
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
//将关闭资源封装在一个方法中,设置参数进行传参
public static void getclose(Connection con,Statement stat,ResultSet res) throws SQLException {
con.close();
stat.close();
res.close();
}
}
然后可以定义一个类来调用工具类:
import com.liu.jdbctool.jdbctool;
import java.sql.*;
public class Test1 {
//这里的抛出异常是为了下面的释放资源而抛出的
public static void main(String[] args) throws SQLException {
Connection connection=null;
Statement statement = null;
ResultSet resultSet=null;
try{
connection =jdbctool.getConnection();
//创建Statement
statement=connection.createStatement();
String sql="select * from product";
//执行sql语句
resultSet=statement.executeQuery(sql);
//通过字段获取已经查询到的内容
while(resultSet.next()){
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("price"));
}
}
catch (Exception e){
e.printStackTrace();
}
finally {
jdbctool.getclose(connection,statement,resultSet);
System.out.println("释放资源");
}
}
}
SQL注入问题:
上面的Statement执行sql语句时可能会造成sql注入,
就是本来应该语句里面加的是条件,字段名等等,但是也可以利用sql语法来添加一些非法的语句,例如:
String a="or 1=1";
String sql="select * from product where id="+a;
resultSet=statement.executeQuery(sql);
这样就会造成数据的泄露,因为这个1=1是真,所以会爆出所有的信息,而不是id=1(任何一个id)的单个信息
所以用一个新的能够执行sql对象的类:PreparedStatement
在一处无意间看到的:
它继承了Statement类,也可以执行sql语句,但是会预编译,就是说将传入进来的参数,系统将不会认为它会是一条SQL语句,而默认其是一个参数,参数中的or或者and 等就不是SQL语法保留字了
用PreparedStatement来操作一下,用Connment直接 . preparedStatement(sql语句),就将进行预编译(其中SQL语句中的?
占位符,而将在预编译时被preparedStatement.setInt(第几个,参数值)
所取代)
import com.liu.jdbctool.jdbctool;
import java.sql.*;
public class Test1 {
public static void main(String[] args) throws SQLException {
Connection connection=null;
Statement statement = null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try{
//使用工具类获得操作sql对象————prepareStatement
connection =jdbctool.getConnection();
//预编译,将sql扔进去,prepareStatement是为了防止sql注入
preparedStatement=connection.prepareStatement("INSERT INTO product(`id`,`price`)values(?,?)");
//给操作sql对象————preparedStatement赋值
preparedStatement.setInt(1,16);
preparedStatement.setInt(2,53000);
//进行真正的编译,并且返回一个修改成功的行数a
int a=preparedStatement.executeUpdate();
if(a!=0){
System.out.println("添加数据成功");
}
}
catch (Exception e){
e.printStackTrace();
}
finally {
jdbctool.getclose(connection,statement,resultSet);
preparedStatement.close();
System.out.println("释放资源");
}
}
}
两种数据库操作并带上添加事务提交:
搜索数据:
import com.liu.jdbctool.jdbctool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class transactionselect {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
connection=jdbctool.getConnection();
connection.setAutoCommit(false);
String sql="select * from product";
preparedStatement=connection.prepareStatement(sql);
//将查询的结果放入结果集resultSet中
resultSet=preparedStatement.executeQuery();
//调用ResultSet类的方法,输入字段的名字显示刚才查询的结果
while(resultSet.next()){
System.out.println(resultSet.getObject("id"));
System.out.println(resultSet.getObject("price"));
}
//开启事务的提交
connection.commit();
System.out.println("事务提交成功");
}
catch (SQLException e){
try{
//显示的定义事务的回滚
connection.rollback();
}
catch (SQLException e1){
e1.printStackTrace();
}
e.printStackTrace();
}
finally {
try{
jdbctool.getclose(connection,preparedStatement,resultSet);
}
catch (SQLException e){
e.printStackTrace();
}
}
}
}
添加数据:
import com.liu.jdbctool.jdbctool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class transactionupdate {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement=null;
try{
//获得Connection
connection= jdbctool.getConnection();
//关闭自动提交事务,但是会自动进入事务
connection.setAutoCommit(false);
String sql="insert into product values (45,66666)";
preparedStatement=connection.prepareStatement(sql);
int a=preparedStatement.executeUpdate();
if(a>0){
System.out.println("已经添加数据成功");
}
//上面的这些代码已经将数据成功添加在其中,但是还没有进行事务提交
//这里的这个错误是为了让下面的事务回滚生效
int sum=1/0;
connection.commit();
System.out.println("事务提交成功");
}
catch (SQLException e){
try{
//事务回滚,这里也可以不添加,因为它默认会进行回滚,
// 这里是显示的进行事务回滚
connection.rollback();
}
catch (SQLException e1){
e1.printStackTrace();
}
e.printStackTrace();
}
finally {
try{
jdbctool.getclose(connection,preparedStatement,null);
}
catch (SQLException e){
e.printStackTrace();
}
}
}
}
总结:说到底,jdbc就是第一步开启驱动,第二步获得用户连接的信息如:url,username等信息,第三步获得Connection(代表数据库),第四步用Connection获得能够执行sql的类Statement或者PreparedStatement (根据上面类的不同,前一个是先获取,后一个是直接将sql语句扔进connection . prepareStatement(sql)中进行预编译) ,第五步用上面获得的类对sql进行增删改查等执行,第六步释放资源
学到框架就简单了,不用手写这些繁杂的东西