Java连接Mysql

发布于:2022-12-22 ⋅ 阅读:(440) ⋅ 点赞:(0)
package itcast;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * Description:需要导入的jar包
 *                 <dependency>
 *                          <groupId>mysql</groupId>
 *                          <artifactId>mysql-connector-java</artifactId>
 *                          <version>5.0.8</version>
 *                 </dependency>
 *                 需要在pom.xml build标签中添加代码
 *                 加载配置文件properties
 *
 *
 */
public class JDBCUtils {
    private static String url;
    private static String password;
    private static String user;
    private static String driver;
    /**
     * 加载配置资源
     */
    static {
        try {
            Properties properties=new Properties();
            URL resource = JDBCUtils.class.getClassLoader().getResource("jdbc.properties");
            String path = resource.getPath();
            InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(resourceAsStream);
            url=properties.getProperty("url");
            user=properties.getProperty("user");
            password=properties.getProperty("password");
            driver=properties.getProperty("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取链接 Connection
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    /**
     * 释放资源
     */
    public static void close(Statement statement,Connection connection){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement statement, Connection connection, ResultSet resultSet){
        close(statement,connection);
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(PreparedStatement preparedStatement,Connection connection){
        if (preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(PreparedStatement preparedStatement, Connection connection, ResultSet resultSet){
        close(preparedStatement,connection);
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
package itcast;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Description:
 */
public class JDBCTextDemo1 {
    public static void main(String[] args) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        Statement statement = connection.createStatement();
//        SQL语句
        String sql1="insert into itcast_01 values (3,'lisi',10086,2000)";
        String sql5="insert into itcast_01 values (?,?,?,?)";
        String sql2="update itcast_01 set id=2,name='itcast02' where id=5";
        String sql3="delete from itcast_01 where id=2";
        String sql4="select *from itcast_01";
//        Statement 存在SQL注入漏洞往往采用PrepareStatement
        executeUpdate 执行DML 语句
//        statement.executeUpdate(sql2);
        executeQuery 执行DQL语句
//        ResultSet resultSet = statement.executeQuery(sql4);
//        while (resultSet.next()){
//            int id = resultSet.getInt(1);
//            String name = resultSet.getString(2);
//            int deptId = resultSet.getInt(3);
//            int salary = resultSet.getInt(4);
//            System.out.println("id:"+id+"name:"+name+"deptId:"+deptId+"salary:"+salary);
//        }
//        JDBCUtils.close(statement,connection,resultSet);
        PreparedStatement preparedStatement = connection.prepareStatement(sql5);
        preparedStatement.setInt(1,8);
        preparedStatement.setString(2,"wang");
        preparedStatement.setInt(3,114);
        preparedStatement.setInt(4,9080);
        preparedStatement.executeUpdate();
        JDBCUtils.close(preparedStatement,connection);
    }
}

jdbc.properties

url=jdbc:mysql://localhost:3306/数据库名
user=root
password=root
driver=com.mysql.jdbc.Driver

网站公告

今日签到

点亮在社区的每一天
去签到