吴裕雄--天生自然JAVA数据库编程:JDBC操作步骤及数据库连接操作

public class ConnectionDemo01{
    // 定义MySQL的数据库驱动程序
    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
    public static void main(String args[]){
        try{
            Class.forName(DBDRIVER) ;    // 加载驱动程序
        }catch(ClassNotFoundException e){
            e.printStackTrace() ;
        }
    }
};
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
public class ConnectionDemo02{
    // 定义MySQL的数据库驱动程序
    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
    // 定义MySQL数据库的连接地址
    public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
    // MySQL数据库的连接用户名
    public static final String DBUSER = "root" ;
    // MySQL数据库的连接密码
    public static final String DBPASS = "mysqladmin" ;
    public static void main(String args[]){
        Connection conn = null ;        // 数据库连接
        try{
            Class.forName(DBDRIVER) ;    // 加载驱动程序
        }catch(ClassNotFoundException e){
            e.printStackTrace() ;
        }
        try{
            conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
        }catch(SQLException e){
            e.printStackTrace() ;
        }
        System.out.println(conn) ;    // 如果此时可以打印表示连接正常
        try{
            conn.close() ;            // 数据库关闭
        }catch(SQLException e){
            e.printStackTrace() ;
        }
    }
};

猜你喜欢

转载自www.cnblogs.com/tszr/p/12158867.html