eclipse连接mysql数据库配置文件

在导入了jar包之后 就要对数据库进行配置

public void init() {//配置连接mysql
        try {
            Class.forName("com.mysql.jdbc.Driver");//1.获取驱动 不能使用 DriverManager.registerDriver(new Driver()); 因为通过查看源码知 这个语句将会注册两次驱动
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String url="jdbc:mysql://127.0.0.1:3306/blog?characterEncoding=utf8";//2.设置要连接的数据库的地址和编码 通常就是127.0.0.1:3306
        String user="root";
        String password="549553";
        try {
            connection=DriverManager.getConnection(url, user, password);//3.获取连接

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            statement=connection.createStatement();//4.创建执行sql的statement对象
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

接下来 就可以使用statement对象的execute方法执行sql语句了

猜你喜欢

转载自blog.csdn.net/qq_39497607/article/details/81674432