Tomcat连接MySQL数据库
新手,写这些东西 ,纯粹是怕自己会忘记,欢迎指导~~~~~
1.加载JDBC数据库驱动程序
访问MySQL的官网进行下载:https://www.mysql.com/products/connector/
选择“JDBC Driver for MySQL”单击download 下载按钮
我用的是 mysql-connector-java-5.1.30-bin.jar,将这个jar包复制到WebContent中的lib里面,右键jar包,构建路径,添加至构建路径
应用程序加载MySQL的JDBC数据库驱动程序代码如下:
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
}
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
}
2.建立连接
Connection con;
try{
String uri="jdbc:mysql://127.0.0.1:3306/test";
String user="root";
String password="root";
con=DriverManager.getConnection(uri,user,password);
}
catch(SQLException e){
System.out.println(e);
}
String uri="jdbc:mysql://127.0.0.1:3306/test";
String user="root";
String password="root";
con=DriverManager.getConnection(uri,user,password);
}
catch(SQLException e){
System.out.println(e);
}
3306为数据库服务器所占用的端口号,没有修改过的话,默认为3306
test为MySQL的建立的数据库的名字,user为用户的id,password为密码
还有简单的增删改查,还不是很熟练,过几天再写