JAVA 连接MySQL
2013年05月06日
⁄ 综合
⁄ 共 1063字 ⁄ 字号
小 中 大
- package DB;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
-
- public class DBconnection {
-
- private static final String driver="com.mysql.jdbc.Driver";
- private static final String url ="jdbc:mysql://localhost:3306/mydata";
- private static final String name = "root";
- private static final String pwd = "accp";
-
-
- public static Connection getCon(){
- Connection con = null;
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(url,name,pwd);
- } catch (ClassNotFoundException e) {
- System.out.println("驱动加载失败!");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("数据库连接失败!");
- e.printStackTrace();
- }
-
- return con;
- }
-
- public static void main(String[] args) {
- Connection con = DBconnection.getCon();
- if(con!=null){
- System.out.println("数据库连接成功!!!!");
- }
-
- }
-
- }
-