mysql数据库的基础知识学习(1)

JDBC:java database connectivity,用java语言来操作数据库。

        (1)测试类创建:modules---创建test文件夹---project structure---modules---sources---选择test文件夹点击一下sources(Mark as处)---ok

       (2)test类快速创建:在上述过程中,把test选择为目录即可,点击tests即可,然后在需要测试的方法alt+enter,根据提示操作。(ctrl+shift+t)

        (3)test类的三个方法before、test、after,执行顺序依序

         (4)预防SQL注入:使用preparedStatement

遇到的一个问题:jdbc的driver没找到,解决方法:在project structure下的libraries中点击“+”,选中对应版本的数据库连接包,如:C:\mysql-connector-java-5.1.38.jar

package study.zx.jdbc;

import java.sql.*;

public class JDBC {
    public static void search() throws ClassNotFoundException, SQLException {
        //1.注册数据库的驱动
        Class.forName ("com.mysql.jdbc.Driver");

        //2.数据库的连接
        String url="jdbc:mysql://localhost:3306/data1";
        String user="root";
        String password="12345";
        Connection connection=DriverManager.getConnection (url,user,password);

        //3.获取操作数据库的对象
 //       Statement statement = connection.createStatement ();


        //查询操作
        String sql="select * from user where username=? and password=?";
        //预防注入
        PreparedStatement preparedStatement = connection.prepareStatement (sql);
        preparedStatement.setString (1,"username");
        preparedStatement.setString (2,"password");
        ResultSet resultSet = preparedStatement.executeQuery ();
        //  ResultSet resultSet = statement.executeQuery (sql);
        //遍历结果集,取出数据
        while(resultSet.next ()){
            String name = resultSet.getString ("username");
            String password1 = resultSet.getString ("password");
            System.out.println("用户名:"+name+",密码是:"+password);
        }
        //5.关闭资源
        resultSet.close ();
      //  statement.close ();
        preparedStatement.close ();
        connection.close ();
    }
    public static void insert() throws ClassNotFoundException, SQLException {
        //1.注册数据库的驱动
        Class.forName ("com.mysql.jdbc.Driver");

        //2.数据库的连接
        String url="jdbc:mysql://localhost:3306/data1";
        String user="root";
        String password="12345";
        Connection connection=DriverManager.getConnection (url,user,password);

        //3.获取操作数据库的对象
        Statement statement = connection.createStatement ();

        //更新操作:添加、修改、删除
        String sql="insert into user values(null,'preston','123',18)";
        statement.executeUpdate (sql);

        //5.关闭资源
        statement.close ();
        connection.close ();
    }
    public static void update() throws ClassNotFoundException, SQLException {
        //1.注册数据库的驱动
        Class.forName ("com.mysql.jdbc.Driver");

        //2.数据库的连接
        String url="jdbc:mysql://localhost:3306/data1";
        String user="root";
        String password="12345";
        Connection connection=DriverManager.getConnection (url,user,password);

        //3.获取操作数据库的对象
        Statement statement = connection.createStatement ();

        //更新操作:添加、修改、删除
        String sql="update user set username='tom',password='123' where id=12";
        statement.executeUpdate (sql);


        //5.关闭资源
        statement.close ();
        connection.close ();
    }
    public static void delete() throws ClassNotFoundException, SQLException {
        //1.注册数据库的驱动
        Class.forName ("com.mysql.jdbc.Driver");

        //2.数据库的连接
        String url="jdbc:mysql://localhost:3306/data1";
        String user="root";
        String password="12345";
        Connection connection=DriverManager.getConnection (url,user,password);

        //3.获取操作数据库的对象
        Statement statement = connection.createStatement ();

        //更新操作:添加、修改、删除
        String sql="delete from user where id=12";
        statement.executeUpdate (sql);

        //5.关闭资源
        statement.close ();
        connection.close ();
    }
}

//测试类
package study.zx.jdbc;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.SQLException;

import static org.junit.Assert.*;

public class JDBCTest {

    @Before
    public void setUp() throws Exception {
        System.out.println ("开始测试");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println ("测试结束");
    }

    @Test
    public void search() throws SQLException, ClassNotFoundException {
        JDBC.search ();
    }

    @Test
    public void insert() throws SQLException, ClassNotFoundException {
        JDBC.insert ();
    }

    @Test
    public void update() throws SQLException, ClassNotFoundException {
        JDBC.update ();
    }

    @Test
    public void delete() throws SQLException, ClassNotFoundException {
        JDBC.delete ();
    }
}

//工具类
//db.propertise
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/data1
username=root
password=12345

//工具类
package study.zx.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static{

        try {
            ClassLoader classLoader = JDBCUtils.class.getClassLoader ();
            InputStream resourceAsStream = classLoader.getResourceAsStream ("db.propertise");
            Properties properties = new Properties ();
            properties.load (resourceAsStream);
            driver = properties.getProperty ("driver");
            url = properties.getProperty ("url");
            username = properties.getProperty ("username");
            password = properties.getProperty ("password");
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        //1.注册数据库的驱动
        Class.forName (driver);
        //2.数据库的连接
        return DriverManager.getConnection (url,username,password);
    }

    public static void close(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        if(connection !=null){
            try {
                connection.close ();
            } catch (SQLException e) {
                e.printStackTrace ();
            }
        }

        if(preparedStatement !=null){
            try {
                preparedStatement.close ();
            } catch (SQLException e) {
                e.printStackTrace ();
            }
        }

        if(resultSet !=null){
            try {
                resultSet.close ();
            } catch (SQLException e) {
                e.printStackTrace ();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/preston555/article/details/110070884