mysql连接代码【经典】

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



Java 连接数据库标准代码


public class mysql {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String url = "jdbc:mysql://localhost:3306/test";
    private static String user = "root";
    private static String password = "root";
    private static Connection con = null;

    private static Connection getConnection() {

        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return con;
    }

    public static void main(String[] args) {
        con = mysql.getConnection();
        if (con != null){
            System.out.println("连接成功!");
        }
    }
}

Ps:Connection con = Mysql.getConnection();//jsp连接数据库代码!

发布了33 篇原创文章 · 获赞 4 · 访问量 6025

猜你喜欢

转载自blog.csdn.net/amspony/article/details/102474374