急!求一个JAVA超市管理系统的源代码,Eclpise编译通过

2024-11-26 10:44:58
推荐回答(1个)
回答1:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Conn {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/blog?user=root&password=root";

//获得数据库连接
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url);
if(conn != null){
System.out.print("成功");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;

}

//关闭Resultset
public static void closeResultSet(ResultSet rs)
{
try
{
if(rs != null)
{
rs.close();
}
}catch (SQLException ex)
{
ex.printStackTrace();
}
}

//关闭Statement
public static void closeStatement(Statement st)
{
try
{
if(st != null)
{
st.close();
}
}catch (SQLException ex)
{
ex.printStackTrace();
}
}

//关闭Connection
public static void closeConnection(Connection conn)
{
try
{
if(conn != null)
{
conn.close();
}
}catch (SQLException ex)
{
ex.printStackTrace();
}
}

//测试数据库连接是否成功
public static void main(String[] args) {
Conn.getConnection();
}

}
采纳我吧!