JDBC连接mysql

一丶JDBC第二种格式

JDBC连接数据库的步骤:

1.加载驱动:Class.forName(“com.mysql.jdbc.Driver”)
2.获取数据库链接: DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/数据库名”)
3.创建statement: conn.createStatement()
4.执行SQL: stat.executeUpdate(“sql语句”)
5.关闭连接:


代码如下:

package com.iflysse.test07;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;



/**
 * driver=com.mysql.jdbc.Driver
 *  url=jdbc:mysql://127.0.0.1:3306/test2
 *  root=root
 *  psw=245645
 *
 *
 */
public class Day041201 {
    @Test
    public void test01() throws SQLException{
        //加载数据库的驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //获取数据库链接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/practice", "root", "123456");
        //创建statement
        Statement stat = conn.createStatement();
        //对数据库进行增、删、改的操作
        int rlt1 = stat.executeUpdate("insert into coures values ('007','体育1',5)");
        if(rlt1 > 0 ){
            System.out.println("添加成功...");
        }else{
            System.out.println("添加失败...");
        }
        //删除的操作
        int rlt2 = stat.executeUpdate("delete from sc where sno ='A1006'");
        if(rlt2 > 0 ){
            System.out.println("删除成功...");
        }else{
            System.out.println("删除失败...");
        }
        //更新的操作
        int rlt3 = stat.executeUpdate("Update sc set grade = 100 where sno = 'A1003'");
        if(rlt3 > 0 ){
            System.out.println("更新成功...");
        }else{
            System.out.println("更新失败...");
        }
        //查询的操作
        ResultSet rs = stat.executeQuery("select * from student");
        while(rs.next()){
            //放入列名
            System.out.println(rs.getString("sno")+ "\t");
            //放入第几列
            System.out.println(rs.getString(2));
        }
    }
}

将JDBC打包为一个工具类:

package jdbc_util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 整合:
 * 加载驱动、获取链接、关闭链接
 */
public class JdbcUtil01 {
    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/practice";
    private static String username = "root";
    private static String password = "123456";
    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn,Statement stat,ResultSet rs){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(stat != null){
            try {
                stat.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

二丶第二种简化重复的步骤

注意:创建一个jdbc.properties的文件,将驱动链接、数据库的地址、用户名、密码 放在此文件下面:
JDBC连接mysql_第1张图片

重新写JDBCUtil包的程序,代码如下:

package jdbc_util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 整合:
 * 加载驱动、获取链接、关闭链接
 */
public class JdbcUtil01 {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;
    static{
        try {
            Properties p = new Properties();

            FileInputStream fis = new FileInputStream(new File("src//jdbc.properties"));
            p.load(fis);
            driver = p.getProperty("driver");
            url = p.getProperty("url");
            username = p.getProperty("username");
            password = p.getProperty("password");

            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn,Statement stat,ResultSet rs){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(stat != null){
            try {
                stat.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

重点代码截图如下:

JDBC连接mysql_第2张图片

你可能感兴趣的:(JDBC连接mysql)