Java连接Mysql

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值