java连接数据库代码解释!!!谢谢大神 200
publicclassDB{publicstaticConnectiongetConn(){Connectionconn=null;try{Class.forName("...
public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/sc", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getState(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try{
rs = stmt.executeQuery(sql);
} catch(SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void close(Connection conn) {
try {
if(conn !=null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt !=null){
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if(rs !=null){
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} 展开
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/sc", "root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getState(Connection conn) {
Statement stmt = null;
try {
if(conn != null) {
stmt = conn.createStatement();
}
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet getResultSet(Statement stmt, String sql) {
ResultSet rs = null;
try{
rs = stmt.executeQuery(sql);
} catch(SQLException e) {
e.printStackTrace();
}
return rs;
}
public static void close(Connection conn) {
try {
if(conn !=null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt !=null){
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if(rs !=null){
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
} 展开
展开全部
这就是个链接数据库的工具类啊。
DB.getResultSet(DB.getConn().getState(),"select * from eval");
拿数据库连接然后执行就行了
DB.getResultSet(DB.getConn().getState(),"select * from eval");
拿数据库连接然后执行就行了
追问
对应的数据库是什么啊 我都没建数据库,,,怎么不报错呢
追答
对应的是 mysql 数据库
localhost 下面 的 sc库
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-05-22
展开全部
Class.forName("com.mysql.jdbc.Driver");加载驱动
conn = DriverManager.getConnection("jdbc:mysql://localhost/sc", "root", "root");建立连接
conn = DriverManager.getConnection("jdbc:mysql://localhost/sc", "root", "root");建立连接
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询