📄 dbconnection.java
字号:
package com.lyh.common;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.*;
public class DBConnection {
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_USER="sa";
private static final String DATABASE_PASSWORD="";
public static Connection getConnection()
{
Connection dbconnection=null;
try
{
Class.forName(DRIVER_CLASS);
dbconnection=DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
}catch(ClassNotFoundException sqlex)
{
System.out.println(sqlex.getMessage());
}
catch(SQLException sqlex)
{
System.out.println(sqlex.getMessage());
System.out.print("meizhaodao");
}
return dbconnection;
}
public static void closeConnection(Connection dbconnection)
{
try{
if(dbconnection!=null&&!dbconnection.isClosed())
{
dbconnection.close();
}
}catch(SQLException sqlEX)
{
System.out.println(sqlEX.getMessage());
}
}
public static void closeResultSet(ResultSet res)
{
try{
if(res!=null){
res.close();
}
}
catch(SQLException sqlEx)
{
sqlEx.printStackTrace();
}
}
public static void closeStatement(PreparedStatement pstatement)
{
try{
if(pstatement!=null)
{
pstatement.close();
pstatement=null;
}
}catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -