java和mysql数据库连接(注意导入jar)

jar下载

http://jarfiles.pandaidea.com/list

mysql数据连接测试

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 
 5 public class DbUtil {
 6     private String dbUrl = "jdbc:mysql://localhost:3306/db_courseSelect";
 7     private String dbUserName = "root";
 8     private String dbPassword = "miao";
 9     private String jdbcName = "com.mysql.jdbc.Driver";
10 
11     /**
12      * 获取数据库连接
13      * 
14      * @return
15      * @throws Exception
16      */
17     public Connection getCon() throws Exception {
18         Class.forName(jdbcName);
19         Connection con = DriverManager.getConnection(dbUrl, dbUserName,
20                 dbPassword);
21         return con;
22     }
23 
24     public void closeCon(Connection con) throws Exception {
25         if (con != null) {
26 
27             con.close();
28         }
29     }
30 
31     public static void main(String[] args) {
32         DbUtil dbUtil = new DbUtil();
33         try {
34             dbUtil.getCon();
35             System.out.println("数据库连接成功!");
36         } catch (Exception e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         }
40     }
41 }
View Code
原文地址:https://www.cnblogs.com/sxmcACM/p/3477585.html