javaServlet技术Javaweb

Servlet之连接mysql

2016-02-21  本文已影响162人  onweer

  1. mysql官网下载 mysql-connector-java-5.1.38jar包.
  2. 项目引入jar包.
    出现 :java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
    解决办法:
    jar包 放在webapps/xxx/WEB-INF/lib 中!
  3. 加载头文件 import java.sql.*;
  4. 定义驱动名 定义URL 用户名 密码...
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库
String url = "jdbc:mysql://127.0.0.1:3306/demo";
// MySQL配置时的用户名密码
String user = "root";
String password = "root";
  1. 连接测试
try{
        //加载驱动程序
        Class.forName(driver);
        // 连接数据路
        Connection conn = DriverManager.getConnection(url,user,password);
        if(!conn.isClosed()){
            System.out.println("Successed connecting to the Database!");
            }
        }catch(Exception e){
            e.printStackTrace();
        }

给个github follow me的链接,上面有很多初学者可供学习的资料,项目.

<a>https://github.com/SuperZee</a>

上一篇 下一篇

猜你喜欢

热点阅读