如何用前台Java连接后台SQL来写鲜花店管理系统
希望有模式图及Java写的代码的注释,好心人帮助的发到我的邮箱:419950791@qq.com谢谢!!!...
希望有模式图及Java写的代码的注释,好心人帮助的发到我的邮箱:419950791@qq.com 谢谢!!!
展开
1个回答
展开全部
首先在SQL数据库中创建相应的数据库,接下来我们创建数据库连接类和连接方法以及关闭连接方法
public class DBConnection {
private static final String DATASOURCE ="restrant";
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;databaseName=restrant";
private static final String DATABASE_USRE="sa";
private static final String DATABASE_PASSWORO="123456";
public static Connection getConnction(){
Connection dbConnection =null;
try{
Class.forName(DRIVER_CLASS);
dbConnection =DriverManager.getConnection(DATABASE_URL,DATABASE_USRE,DATABASE_PASSWORO);
}catch(Exception e){
e.printStackTrace();
}
return dbConnection;
}
public static void closeConnection(Connection dbConnection){
try{
if(dbConnection != null && (!dbConnection.isClosed())){
dbConnection.close();
}
}catch(SQLException sqlEx){
sqlEx.printStackTrace();
}
}
public static void closeResultSet(ResultSet res){
try{
if(res != null){
res.close();
res = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
public static void closeStatement(PreparedStatement pStatement){
try{
if(pStatement != null){
pStatement.close();
pStatement=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
public class DBConnection {
private static final String DATASOURCE ="restrant";
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DATABASE_URL="jdbc:sqlserver://localhost:1433;databaseName=restrant";
private static final String DATABASE_USRE="sa";
private static final String DATABASE_PASSWORO="123456";
public static Connection getConnction(){
Connection dbConnection =null;
try{
Class.forName(DRIVER_CLASS);
dbConnection =DriverManager.getConnection(DATABASE_URL,DATABASE_USRE,DATABASE_PASSWORO);
}catch(Exception e){
e.printStackTrace();
}
return dbConnection;
}
public static void closeConnection(Connection dbConnection){
try{
if(dbConnection != null && (!dbConnection.isClosed())){
dbConnection.close();
}
}catch(SQLException sqlEx){
sqlEx.printStackTrace();
}
}
public static void closeResultSet(ResultSet res){
try{
if(res != null){
res.close();
res = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
public static void closeStatement(PreparedStatement pStatement){
try{
if(pStatement != null){
pStatement.close();
pStatement=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询