package DBManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn {
Connection conn;
public Connection getConnection(){//目的是为了获取与数据库的连接,在主方法中调用该方法。
//JDBC技术是一种执行SQL语句的javaAPI。
try {
Class.forName("com.mysql.jdbc.Driver");//java.lang包的静态方法来加载JDBC驱动程序。。加载失败异常ClassNotFoundException
System.out.println("db run success");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/littlet","root","1234");//通过访问数据库的URL获取数据库连接对象java.sql包中类DriveManager的静态方法。。连接失败异常SQLException
System.out.println("db connect success");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭Connection对象
public static void closeConnection(Connection conn){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//回滚
public static void rollbackConnection(Connection conn){
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
|