當前位置:首頁 » 操作系統 » java財務系統源碼

java財務系統源碼

發布時間: 2024-09-06 16:20:24

❶ 求:用java語言編寫的銀行家演算法的源代碼

import java.util.*;

class ThreadTest {
static int type = 4, num = 10; //定義資源數目和線程數目
static int[] resource = new int[type]; //系統資源總數
//static int[] Resource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //線程組
Bank temp = new Bank();

public void init() {
//初始化組中每個線程,隨機填充系統資源總數
for(int i = 0; i < type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i < bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}

class Bank extends Thread {
//銀行家演算法避免死鎖
public int[]
max = new int[type], //總共需求量
need = new int[type], //尚需資源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申請資源量
Resource = new int[type]; //資源副本
private boolean isFinish = false; //線程是否完成
int[][] table = new int[bank.length][type*4]; //二維資源分配表

private void init() {
// 隨機填充總共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i < type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //從系統資源中減去已分配的
}
printer();
for(int i = 0; i < type; i++) {
if(resource[i] < 0) {
//若出現已分配量超出系統資源總數的錯誤則退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}

public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}

public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序沒有完成時一直不斷申請資源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段時間模擬程序運行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//運行結束釋放佔有資源
for(int i = 0; i < type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}

private void printer() {
//列印當前資源信息
System.out.print(getName() + " Max:");
for(int i = 0; i < type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i < type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i < type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//隨機產生申請資源量並檢測是否超標
boolean canAsk = false;
for(int i = 0; i < type; i++) {
request[i] = rand.nextInt(20);
//防止申請量超過所需量
if(request[i] > need[i])
request[i] = need[i];
}
for(int i = 0; i < type; i++) //防止隨機申請資源全為0
if(request[i] > 0)
canAsk = true;
synchronized(resource) {
//鎖住可供資源檢查是否超標
for(int i = 0; i < type; i++) {
if(request[i] > resource[i])
//如果申請資源超過可供資源則等待一段時間後重新申請
return false;
}
}
return canAsk;
}
private void tryRequest() {
//創建副本嘗試分配請求
synchronized(resource) {
for(int i = 0; i < type; i++)
//依然要防止請求量超出范圍
if(request[i] > resource[i])
return;
for(int i = 0; i < type; i++) {
//復制資源量並減去需求量到一個副本上
Resource[i] = resource[i];
Resource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i < type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果檢查安全則將副本值賦給資源量並修改佔有量和需求量
for(int i = 0; i < type; i++) {
resource[i] = Resource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//銀行家演算法檢查安全性
synchronized(bank) {
//將線程資源信息放入二維資源分配表檢查安全性,0~type可用資源/type~type*2所需資源/type*2~type*3佔有資源/type*3~-1可用+佔用資源
for(int i = 0; i < bank.length; i++) {
for(int j = type; j < type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j < type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求資源從小到大排
for(int i = 0; i < bank.length; i++) {
for(int j = i; j < bank.length-1; j++) {
sort(j, 4);
}
}
//進行此時刻的安全性檢查
for(int i = 0; i < type; i++) {
table[0][i] = Resource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] < table[1][i+type])
return false;
}
for(int j = 1; j < bank.length-1; j++) {
for(int k = 0; k < type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] < table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//遞歸冒泡排序
int tempNum;
if(table[j][k] > table[j+1][k]) {
for(int i = type; i < type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] && k < type*2) //此資源量相同時遞歸下一個資源量排序並且防止超出范圍
sort(j, k+1);
}
private boolean noNeed() {
//是否還需要資源
boolean finish = true;
for(int i = 0; i < type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}

public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//後台線程,設定程序運行多長時間後自動結束
new Timeout(30000, "---Stop!!!---");
}
}

❷ Shark(鯊魚記賬系統)–附源碼

Shark(鯊魚記賬系統)–附源碼

package com.shayu.note;

import java.util.Scanner;

public class SharkSystem {

// //下面開始循環
while (flag) {
//或蔽開機界面
System.out.println("-----歡迎使用鯊魚記賬系統-----");
System.out.println("1,收支明細");
System.out.println("2,登記收入");
System.out.println("3,登記支出");
System.out.println("4,退出系統");
System.out.println("請按照提衫鄭州示選擇你要使叢孝用的的功能--");
Scanner scanner = new Scanner(System.in);//判斷用戶輸入是否符合要求
int choice = scanner.nextInt();//選擇
//檢測用戶輸入的數據並返回相應的請求結果
while (choice != 1 & choice != 2 & choice != 3 & choice != 4) {
System.out.println("請重新輸入--");
int newchoice = scanner.nextInt();
choice = newchoice;
}

}

package com.shayu.note;

import java.util.Scanner;

//鯊魚記賬系統
public class Shark {
public static void main(String[] args) {
SharkSystem shark=new SharkSystem();
shark.StartSystem();
}

}

❸ 求編寫一個超級簡單的Java的程序源代碼

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Caculator extends JFrame implements ActionListener,KeyListener{
private JTextField tf=new JTextField();
private float x=0;
private float y=0;
private int code=0;
private boolean enable;
private boolean first;
private String str="";
public Caculator(){
Container ct=this.getContentPane();
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
tf.setHorizontalAlignment(JTextField.RIGHT);
//tf.setText("0");
enable=true;
first=true;
ct.add(tf,BorderLayout.NORTH);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,4));
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(Caculator.this,"確定要關閉程序嗎?","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){
e.getWindow().setVisible(false);
e.getWindow().dispose();
System.exit(0);
}

}
});
Button btn=null;
btn=new Button("1");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("2");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("3");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("+");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("4");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("5");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("6");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("-");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("7");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("8");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("9");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("*");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("0");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button(".");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("/");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
btn=new Button("=");
panel.add(btn);
btn.addActionListener(this);
btn.addKeyListener(this);
this.add(panel,BorderLayout.CENTER);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Caculator mainframe=new Caculator();
mainframe.setTitle("testing Caculator");
mainframe.setSize(400,400);
mainframe.setVisible(true);

}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand()=="+"){
x= Float.parseFloat(tf.getText());
code=0;
this.tf.setText("");

}
if(e.getActionCommand()=="-"){
x= Float.parseFloat(tf.getText());
code=1;
this.tf.setText("");

}
if(e.getActionCommand()=="*"){
x= Float.parseFloat(tf.getText());
code=2;
this.tf.setText("");

}
if(e.getActionCommand()=="/"){
x= Float.parseFloat(tf.getText());
code=3;
this.tf.setText("");

}

if(e.getActionCommand()!="+"&&e.getActionCommand()!="-"&&e.getActionCommand()!="*"&&e.getActionCommand()!="/"&&e.getActionCommand()!="="){
if(enable){
if(first){
System.out.println("haha");
tf.setText(e.getActionCommand());
first=false;
}
else {
tf.setText(tf.getText()+e.getActionCommand());
}

}

else {
tf.setText(e.getActionCommand());
enable=true;

}
}
if(e.getActionCommand()=="="){
switch(code){
case 0:
y=x+Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
case 1:
y=x-Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
case 2:
y=x*Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
case 3:
y=x/Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
}

}

}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyChar()=='+'){
x= Float.parseFloat(tf.getText());
code=0;
this.tf.setText("");

}
if(e.getKeyChar()=='-'){
x= Float.parseFloat(tf.getText());
code=1;
this.tf.setText("");

}
if(e.getKeyChar()=='*'){
x= Float.parseFloat(tf.getText());
code=2;
this.tf.setText("");

}
if(e.getKeyChar()=='/'){
x= Float.parseFloat(tf.getText());
code=3;
this.tf.setText("");

}

if(e.getKeyChar()=='1'||e.getKeyChar()=='2'||e.getKeyChar()=='3'||e.getKeyChar()=='0'
||e.getKeyChar()=='4'||e.getKeyChar()=='5'||e.getKeyChar()=='6'||e.getKeyChar()=='.'
||e.getKeyChar()=='7'||e.getKeyChar()=='8'||e.getKeyChar()=='9'){
System.out.println("hai");
if(enable){
if(first){
System.out.println("hehe");
str=Character.toString(e.getKeyChar());
tf.setText(str);
first=false;
}
else {

str=Character.toString(e.getKeyChar());
tf.setText(tf.getText()+str);
}

}

else {

str=Character.toString(e.getKeyChar());
tf.setText(str);
enable=true;

}
}
if(e.getKeyCode()==KeyEvent.VK_ENTER){
switch(code){
case 0:
y=x+Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
case 1:
y=x-Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
case 2:
y=x*Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
case 3:
y=x/Float.parseFloat(this.tf.getText());
tf.setText(Float.toString(y));
enable=false;
break;
}

}
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}

❹ 求個好用的java網上商城源碼。

我們的項目是基於shop++二次開發的,僅供參考~

❺ 求編寫一個超級簡單的Java的程序源代碼

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login {

public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}

class LoginFrm extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel("用戶名:");
JLabel pwdLabel=new JLabel("密碼:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton butnSure=new JButton("確定");
JButton butnCancel=new JButton("取消");
public LoginFrm() {
super("登陸");
setBounds(500, 200, 280, 220);
setVisible(true);
setLayout(null);
nameLabel.setBounds(45, 20, 100, 25);
add(nameLabel);
add(name);
name.setBounds(105, 20, 110, 25);
add(pwdLabel);
pwdLabel.setBounds(45, 60, 100, 25);
add(password);
password.setBounds(105, 60, 110, 25);
add(butnSure);
butnSure.setBounds(45, 100, 80, 25);
add(butnCancel);
butnCancel.setBounds(135, 100, 80, 25);
butnSure.addActionListener(this);
butnCancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新橡猜
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() ==butnSure){
System.out.println("用戶名:"+name.getText());
System.out.println("密碼:"+name.getText());
if("admin".equals(name.getText().trim())&&"123".equals(password.getText().trim())){
this.dispose();
new MainFrm("碼迅用戶界面",name.getText().trim(),password.getText().trim());
}else {
JOptionPane.showMessageDialog(this, "用遲如此戶不存在");
}
}else if(e.getSource()==butnCancel){
System.exit(1);
}
}

class MainFrm extends JFrame{
private JLabel info;

public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陸成功,用戶名:"+name+",密碼:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
}

❻ 什麼是java源代碼 怎麼查看

不知道你說的是瀏覽器的還是什麼的,
如果是瀏覽器的那麼簡單找到工具-查看源代碼,你就能看見代碼了,
還有一個就是被編譯成class文件的java用反編譯工具可以看到源代碼,
如果以上都不是你想要的答案,那麼你所說的代碼就是程序員寫好的代碼文件

❼ 用java來編寫一個銀行類賬戶和信用卡類的賬戶,信用卡類賬戶繼承銀行賬戶,求源碼

public class Account {

protected String accId;
protected String name;
protected double money;

public Account (String accId,String name){
this(accId,name,0);
}

public Account (String accId,String name,double money){
this.accId = accId;
this.name = name;
this.money = money;
}

public void saveMoney(double money){
if(money <= 0){
System.out.println("存款金額必須大於0");
}
this.money += money;
System.out.println("存款成功");
}

public double getMoney(double money){
if(money <= 0){
System.out.println("取款金額必須大於0");
return 0;
}
if(this.money <= money){
System.out.println("余額不足,無法取款");
return 0;
}
this.money -= money;
System.out.println("取款成功");
return money;
}

public double getBalance(){
return this.money;
}

protected double getOverdraft(){
return 0;
}

// 實現了equals方法,list比較時才能正確
@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(this == obj){
return true;
}
if(obj instanceof Account){
return this.accId.equals(((Account)obj).accId);
}
return false;
}

@Override
public String toString() {
return "賬戶=" + accId + ",名字=" + name + ",余額=" + money;
}

}
public class Bank {

// Account實現了equals方法,list查找時才能正確
private List<Account> usersAccounts;

public Bank() {
usersAccounts = new ArrayList<Account>();
}

public void addAccount(Account account) {
if (usersAccounts.contains(account)) {
System.out.println("添加失敗,不能添加同樣的賬戶");
return;
}
usersAccounts.add(account);
}

public boolean delAccount(Account account) {
return usersAccounts.remove(account);
}

public boolean delAccount(String accId) {
return delAccount(new Account(accId, null));
}

public boolean existAccount(Account account) {
return usersAccounts.contains(account);
}

public boolean existAccount(String accId) {
return existAccount(new Account(accId, null));
}

public Account getAccount(String accId){
return usersAccounts.get(usersAccounts.indexOf(new Account(accId, null)));
}

public double getAllMoney() {
// 不考慮是否溢出,只是把所有用戶余額相加
double result = 0;
for (Account account : usersAccounts) {
result += account.getBalance();
}
return result;
}

public double getAllOverdraft() {
// 不考慮是否溢出
double result = 0;
for (Account account : usersAccounts) {
result += account.getOverdraft();
}
return result;
}

public int getAccountNum() {
return usersAccounts.size();
}

public int getCreditAccountNum() {
int num = 0;
for (Account account : usersAccounts) {
// instanceof 性能沒有簡單的方法快。
if (account instanceof CreditAccount) {
num++;
}
}
return num;
}

public int getSavingAccountNum() {
int num = 0;
for (Account account : usersAccounts) {
if (account instanceof SavingAccount) {
num++;
}
}
return num;
}

public List<Account> getAllAccount() {
return usersAccounts;
}
}
public class CreditAccount extends Account{

private double overdraft;

public CreditAccount(String accId,String name){
super(accId, name);
this.overdraft = 1000;
}

public CreditAccount(String accId,String name,double money){
this(accId, name,money,1000);
}

public CreditAccount(String accId,String name,double money,double overdraft){
super(accId, name,money);
this.overdraft = overdraft;
}

@Override
public double getMoney(double money) {
if(money <= 0){
System.out.println("取款金額必須大於0");
return 0;
}
if(this.money + overdraft <= money){
System.out.println("余額不足,無法取款");
return 0;
}
this.money -= money;
System.out.println("取款成功");
return money;
}

@Override
public double getOverdraft(){
return overdraft;
}

@Override
public String toString() {
return "賬戶=" + accId + ",名字=" + name + ",余額=" + money + ",透支=" + overdraft;
}
}
public class SavingAccount extends Account {

public SavingAccount(String accId, String name) {
super(accId, name);
}

public SavingAccount(String accId, String name, double money) {
super(accId, name, money);
}

@Override
public double getMoney(double money) {
return super.getMoney(money);
}

@Override
public double getOverdraft() {
return super.getOverdraft();
}

}
public class Test {

private static Bank bank = new Bank();
public static void main(String[] args) {
Test.genAccount();
// 開戶
Account a1 = new CreditAccount("1", "1", 200, 2000);
Account a2 = new SavingAccount("2", "2", 300);
Account a3 = new SavingAccount("3", "3", 400);
Account a4 = new CreditAccount("4", "4", 500, 2000);
Account a5 = new CreditAccount("4", "5", 600, 2000); // 帳號4重
bank.addAccount(a1);
bank.addAccount(a2);
bank.addAccount(a3);
bank.addAccount(a4);
bank.addAccount(a5);
showNowAccount();
// 銷戶
bank.delAccount("1");
bank.delAccount("2");
showNowAccount();
// 存款
if(bank.existAccount("3")){
Account acc = bank.getAccount("3");
acc.saveMoney(100);
}
showNowAccount();
// 取款
if(bank.existAccount("3")){
Account acc = bank.getAccount("3");
System.out.println("余額=" + acc.getBalance());
acc.getMoney(100);
System.out.println("余額=" + acc.getBalance());
acc.getMoney(1000);
System.out.println("余額=" + acc.getBalance());
}
if(bank.existAccount("4")){
Account acc = bank.getAccount("4");
System.out.println("余額=" + acc.getBalance());
acc.getMoney(100);
System.out.println("余額=" + acc.getBalance());
acc.getMoney(1000);
System.out.println("余額=" + acc.getBalance());
acc.getMoney(10000);
System.out.println("余額=" + acc.getBalance());
}
System.out.println(bank.getAccountNum());
System.out.println(bank.getAllMoney());
System.out.println(bank.getAllOverdraft());
System.out.println(bank.getCreditAccountNum());
System.out.println(bank.getSavingAccountNum());
}

public static void genAccount(){
String s = "1000 0000 0000 000";
Account a = null;
for(int i = 1; i < 11; i ++){
if((i & 2) == 0){
a = new CreditAccount(s + String.valueOf(i), "賬戶" + String.valueOf(i));
} else {
a = new SavingAccount(s + String.valueOf(i), "賬戶" + String.valueOf(i));
}
bank.addAccount(a);
}
}

public static void showNowAccount(){
for(Account account : bank.getAllAccount()){
System.out.println(account);
}
}
}

❽ 跪求一個小型超市管理系統,用java編寫的,急~ 只要源代碼。。可以發我郵箱

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();
}

}

熱點內容
編譯不能顯示 發布:2024-11-24 23:40:52 瀏覽:698
人體編程 發布:2024-11-24 23:40:51 瀏覽:304
謎妹緩存的文件在哪 發布:2024-11-24 23:38:12 瀏覽:146
伺服器自動清理緩存 發布:2024-11-24 23:37:14 瀏覽:663
中國移動網路如何查密碼 發布:2024-11-24 23:37:06 瀏覽:581
計算機資料庫試題 發布:2024-11-24 23:30:17 瀏覽:174
聯想雲教室連接不上伺服器 發布:2024-11-24 23:24:31 瀏覽:895
七七源碼 發布:2024-11-24 22:47:20 瀏覽:677
請訪問其他頁面 發布:2024-11-24 22:46:09 瀏覽:556
愛丟了編程 發布:2024-11-24 22:32:36 瀏覽:110