普通的数据库连接类

时间:2022-09-24 11:58:13
public class DBManage
    {
        public static final String DRIVERNAME = "org.gjt.mm.mysql.Driver";

        public static final String URL = "jdbc:mysql://127.0.0.1:3306/manage";
 
        // 获得一个数据库连接
        public static Connection getConnection()
        {
            Connection conn = null;
            try
            {
                Class.forName(DRIVERNAME);
            }
            catch (ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            try
            {
                conn = DriverManager.getConnection(URL, "root", "root");
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
            return conn;
        }
 
 
        public static void main(String[] args)
        {
           //  测试数据库连接是否成功,如果conn的值不为null,则表示数据库连接成功
            Connection conn = DBManage.getConeection();
            System.out.println("conn = " + conn);
        }
    }
阅读(819) | 评论(0) | 转发(0) |
0

上一篇:JDK 环境变量

下一篇:String[] 与 ArrayList 之间的转换

相关热门文章 给主人留下些什么吧!~~ 评论热议