java Web最简单的功能就是增删改查了,那么第一步就是连接数据库了,虽然很简单,但是好记性不如烂笔头,写下来闲着无聊看一眼
package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
public static Connection getConnection(){
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/students","root","123456" );
} catch (ClassNotFoundException e) {
System.out.println("没有找到数据库驱动");
} catch (SQLException e) {
System.out.println("数据库连接失败");
}
return con;
}
public static void close(Connection con){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
System.out.println("数据库关闭异常");
}
}
}
}