Java连接sql数据库



// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name:   ConnDB.java

package shopBeans;

import java.io.PrintStream;
import java.sql.*;

public class ConnDB
{

 public Connection conn;
 public Statement stmt;
 public ResultSet rs;
 private static String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 private static String dbUrl = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ShopSystem";
 private static String dbUser = "sa";
 private static String dbPwd = "";

 public ConnDB()
 {
  conn = null;
  stmt = null;
  rs = null;
 }

 public static Connection getConnection()
 {
  Connection connection = null;
  try
  {
   Class.forName(dbDriver);
   connection = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
  }
  catch (Exception exception)
  {
   exception.printStackTrace();
  }
  if (connection == null)
   System.err.println("警告:数据库连接失败!");
  return connection;
 }

 public ResultSet doQuery(String s)
 {
  try
  {
   conn = getConnection();
   stmt = conn.createStatement(1004, 1007);
   rs = stmt.executeQuery(s);
  }
  catch (SQLException sqlexception)
  {
   sqlexception.printStackTrace();
  }
  return rs;
 }

 public int doUpdate(String s)
 {
  int i = 0;
  try
  {
   conn = getConnection();
   stmt = conn.createStatement(1004, 1007);
   i = stmt.executeUpdate(s);
  }
  catch (SQLException sqlexception)
  {
   i = 0;
  }
  return i;
 }

 public void closeConnection()
 {
  try
  {
   if (rs != null)
    rs.close();
  }
  catch (Exception exception)
  {
   exception.printStackTrace();
  }
  try
  {
   if (stmt != null)
    stmt.close();
  }
  catch (Exception exception1)
  {
   exception1.printStackTrace();
  }
  try
  {
   if (conn != null)
    conn.close();
  }
  catch (Exception exception2)
  {
   exception2.printStackTrace();
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值