⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db.java_

📁 一个MMORPG手机游戏的服务器端程序源代码
💻 JAVA_
字号:
package com.hypefiend.zsw_mmorpg.server;
import java.sql.*;
import java.util.Properties;
/**
 * Created by IntelliJ IDEA.
 * User: cu
 * Date: 2005-11-4
 * Time: 17:30:31
 * To change this template use File | Settings | File Templates.
 */

import java.sql.*;
import java.util.Vector;
public class DB {
    private static Connection dbGetCon() {
        Connection con = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
        catch (ClassNotFoundException e) {
            System.out.println(e);
        }
        String url = "jdbc:odbc:LocalServer";
        try {
            con = DriverManager.getConnection(url, "zsw", "zsw");
        }
        catch (SQLException e) {
            System.out.println(e);
        }
        return con;
    }

    private static void dbCloseCon(ResultSet rs, Statement st, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            }
            catch (SQLException e) {
                System.out.println(e);
            }
        }

        if (st != null) {
            try {
                st.close();
            }
            catch (SQLException e) {
                System.out.println(e);
            }
        }

        if (con != null) {
            try {
                con.close();
            }
            catch (SQLException e) {
                System.out.println(e);
            }
        }
    }

    public static String dbSelect(String stm) {
        String result = null;
        Connection con = dbGetCon();
        if (con != null) {
            Statement st = null;
            ResultSet rs = null;
            try {
                st = con.createStatement();
                rs = st.executeQuery(stm);
                if (rs.next()) {
                    result = rs.getString(1);
                }
            }
            catch (SQLException e) {
                System.out.println(e);
            }
            finally {
                dbCloseCon(rs, st, con);
            }
        }
        return result;
    }

    public static Vector[] dbTableSelect(String stm) {
        int i;
        Connection con = dbGetCon();
        Vector result[] = new Vector[2];
        result[0] = new Vector();
        result[1] = new Vector();
        if (con != null) {
            Statement st = null;
            ResultSet rs = null;
            try {
                st = con.createStatement();
                rs = st.executeQuery(stm);
                ResultSetMetaData rsmd = rs.getMetaData();
                for (i = 1; i <= rsmd.getColumnCount(); i++)
                    result[1].addElement(rsmd.getColumnName(i));
                while (rs.next()) {
                    Vector row = new Vector();
                    for (i = 1; i <= rsmd.getColumnCount(); i++)
                        row.addElement(rs.getString(i));
                    result[0].addElement(row);
                }
            }
            catch (SQLException e) {
                System.out.println(e);
            }
            finally {
                dbCloseCon(rs, st, con);
            }
        }
        return result;
    }
}

/*
public class DB {

public void db_connection() {
try {
  Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  String url = "jdbc:mysql://localhost/myDB?user=soft&" +
          "password=soft1234&useUnicode=true&characterEncoding=8859_1";
//myDB为数据库名
  Connection conn = DriverManager.getConnection(url);
} catch (InstantiationException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
} catch (ClassNotFoundException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
} catch (SQLException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
}
public void ss()
{ /*
try {
  ReplicationDriver driver = new ReplicationDriver();

  Properties props = new Properties();

  // We want this for failover on the slaves
  props.put("autoReconnect", "true");

  // We want to load balance between the slaves
  props.put("roundRobinLoadBalance", "true");

  props.put("user", "foo");
  props.put("password", "bar");

  //
  // Looks like a normal MySQL JDBC url, with a comma-separated list
  // of hosts, the first being the 'master', the rest being any number
  // of slaves that the driver will load balance against
  //

  Connection conn =  driver.connect("jdbc:mysql://master,slave1,slave2,slave3/test", props);

  //
  // Perform read/write work on the master
  // by setting the read-only flag to "false"
  //
  conn.setReadOnly(false);
  conn.setAutoCommit(false);
  conn.createStatement().executeUpdate("UPDATE some_table ....");
  conn.commit();

  //
  // Now, do a query from a slave, the driver automatically picks one
  // from the list
  //
  conn.setReadOnly(true);

  ResultSet rs = conn.createStatement().executeQuery("SELECT a,b,c FROM some_other_table");
} catch (SQLException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}  */
/*  }
public void ssd()
{
    Connection con;
    PreparedStatement updStmt;
    Statement dispStmt;

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc;webdemo");
        updStmt = con.prepareStatement("insert into Visitors(Name,Email) values(?,?)");
        dispStmt = con.createStatement();

        ResultSet rs =dispStmt.executeQuery("select * from Visitors");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (SQLException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }

}
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -