jdbc连接mysql基操

mysql驱动如下:

链接:https://pan.baidu.com/s/1vo91DbAiIJlIy4I4MbqvNw
提取码:khm4

        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/my_db_01";
        String user = "root";
         String password = "admin123";
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);

或者写一个文件.properites
文件内容如下:
url = jdbc:mysql://localhost:3306/my_db_01
user = root
password = admin123
driver=com.mysql.cj.jdbc.Driver

然后写一个方法去连接mysql

@Test()
    public  void connect05() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properites"));
        String user = properties.getProperty("user");
        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        DriverManager.getConnection(url,user,password);

        System.out.println(user);
    }
package jdbctest_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @author 朱兴
 * @version 1.0
 */
public class ResultSet_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properites"));
        String user = properties.getProperty("user");
        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
//      1.注册驱动
        Class.forName(driver);
//      2.得到连接
        Connection connection = DriverManager.getConnection(url, user, password);
//      3.得到statement
        Statement statement = connection.createStatement();
//      4.组织sql
        String sql="select * from users";
//        执行给定的sql语句,该语句返回当resultset对象
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println(resultSet);
//        5.使用while取出数据
        while (resultSet.next()){
            int id = resultSet.getInt(1);//取出第一列
            String username = resultSet.getString(2);//取出第二列
            String password_ = resultSet.getString(3);//取出第三列
            System.out.println(id+"\t"+username+"\t"+password_+"\t");

        }
//        System.out.println(user);
//        关闭连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}

读取如下

com.mysql.cj.jdbc.result.ResultSetImpl@1a052a00
1	zx	123456	
2	ls	123456	
3	xh	000000	
5	xm	222222	
6	spider	123123	
9	tony	0123456	
10	aa	aaaaaa	

因为使用statement会出现sql注入十分危险
所以用preparestatement
代码如下

package jdbctest_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 朱兴
 * @version 1.0
 */
public class preparedStatement_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入username");
        String uername=scanner.nextLine();
        System.out.println("请输入password");
        String password_=scanner.nextLine();
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properites"));
        String user = properties.getProperty("user");
        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
//      1.注册驱动
        Class.forName(driver);
//      2.得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

//      4.组织sql
        String sql="select count(*) from users where username=? and password=?";
//      3.得到statement
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,uername);
        statement.setString(2,password_);
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
//        System.out.println(user);
//        关闭连接
        resultSet.close();

        statement.close();
        connection.close();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碑无名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值