jsp连接mysql

jsp连接数据库有几种,我选择的是jdbc连接,跨平台,但是需要提前下好相应的驱动(jar包),百度即可

然后 1. 将jar包放在 tomcat/lib/文件夹下

  2.另一个包需要在工程下导入,我是myeclipse导入的,具体过程可百度

以下是连接代码:

            //之前需要导入  import java.sql.*;
Connection con=null; Statement stmt = null;
            //前面的基本不变,localhost 端口号 根据情况做改变 /mydatabase 是数据库的名字
         final String DBURL = "jdbc:mysql://localhost:3306/mydatabase"; 
       //用户名
final String DBUSER = "root";
       //密码
final String DBPWD = "123456"; try{ Class.forName("com.mysql.jdbc.Driver");// load the Connecting class con = DriverManager.getConnection(DBURL, DBUSER, DBPWD); }catch(Exception e){ out.print("连接失败!!<br>"+
e.toString()); }

 

posted @ 2015-10-31 22:14  Gaikin  阅读(260)  评论(0编辑  收藏  举报