`
Elvin.Chu
  • 浏览: 15116 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
JDBC连接Oracle实例
package com.test.oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConfJdbc {
	/**
	 * 连接数据库
	 */
	public static Connection connect() {
		try {
			//注册JDBC驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException ex) {
			System.out.println("数据库驱动没找到...");
		}
		//创建数据库连接
		String url = "jdbc:oracle:thin:@3.242.165.133:1521:pat";
		String username = "patlocal";
		String password = "patlocal";
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, username, password);
			System.out.println(con+" DB连接成功...");
		} catch (SQLException ex) {
			System.out.println("Db连接失败...");
		}
		return con;
	}
	/**
	 * 建表
	 */
	public static void create() {
		Connection con = connect();
    	Statement statement = null;
    	try {
    		statement = con.createStatement();
            System.out.println("--Creating Tables--");
            statement.executeUpdate("CREATE TABLE Person(name VARCHAR2(100) PRIMARY KEY,age INTEGER,live_in VARCHAR2(100) )");
            statement.close();
            con.close();
            System.out.println("建表成功...");
    	} catch (SQLException ex) {
    		System.out.println("建表失败...");
    	}
	}
	/**
	 * 插入记录
	 */
	public static void insert() {
		Connection con = connect();
    	Statement statement = null;
    	try {
    		statement = con.createStatement();
    		System.out.println("--Inserting Data--");
            statement.executeUpdate("INSERT INTO Person (name, age, live_in) VALUES('Robert Bellamy',24,'England')");
            statement.executeUpdate("INSERT INTO Person (name, age, live_in) VALUES ('Grayham Downer',null,'Africa')");
            statement.executeUpdate("INSERT INTO Person (name, age, live_in) VALUES ('Timothy French',24,'Africa')");
            statement.executeUpdate("INSERT INTO Person (name, age, live_in) VALUES ('Butch Fad',53,'USA')");
            statement.executeUpdate("INSERT INTO Person (name, age, live_in) VALUES ('Judith Brown',34,'Africa')");
            statement.close();
            con.close();
            System.out.println("插入成功...");
    	} catch (SQLException ex) {
    		System.out.println("插入失败...");
    	}
		
        
	}
	/**
	 * 查询数据
	 */
	public static void select() {
		Connection con = connect();
    	Statement statement = null;
    	try {
    		statement = con.createStatement();
    		ResultSet result;
            System.out.println("--SQL queries--");
            result = statement.executeQuery("SELECT AVG(age) FROM Person");
            if (result.next()) {
                System.out.println("AV.age: " + result.getDouble(1));
            }
            System.out.println();
            result = statement.executeQuery("SELECT name FROM Person WHERE live_in = 'Africa'");
            while (result.next()) {
                System.out.println("Person's name:" + result.getString(1));
            }
            statement.close();
            con.close();
    	} catch (SQLException ex) {
    		System.out.println("查询失败...");
    	}
		
	}
	/**
	 * 删除数据
	 */
	public static void delete() {
		Connection con = connect();
    	Statement statement = null;
    	try {
    		statement = con.createStatement();
            statement.executeUpdate("DELETE FROM Person WHERE name='Timothy French'");
            statement.close();
            con.close();
            System.out.println("删除成功...");
    	} catch (SQLException ex) {
    		System.out.println("删除失败...");
    	}
    	System.out.println("-- Now data --");
    	select();
		
	}
	/**
	 * 删除表
	 */
	public static void dropTable() {
		Connection con = connect();
    	Statement statement = null;
    	try {
    		statement = con.createStatement();
    		//删除表
            statement.execute("DROP TABLE Person");
            statement.close();
            con.close();
            System.out.println("表已删除...");
    	} catch (SQLException ex) {
    		System.out.println("删表失败...");
    	}
		
	}
	
	public static void main(String[] arg) {
		connect();
//        create();
//		insert();
//		select();
//		delete();
//		dropTable();
    }
	


}
java调用服务器端exe可执行文件
package test;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class JavaExe {

  @SuppressWarnings("unchecked")
 public static void main(String[] args) {
 
  Date inGroupTime = new Date();
  java.util.Calendar cal = Calendar.getInstance();  
  cal.setTime(inGroupTime);
  int day_of_month = cal.get(Calendar.DAY_OF_MONTH);  
  System.out.println(day_of_month);
  System.out.println(day_of_month%2!=0);
  
  Map<Integer,String> map = new HashMap<Integer,String>();
  map.put(1, "1");
  map.put(2,"1");
  map.put(1, "1");
  for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {  
    
      Object key = iter.next();  
      Object val = map.get(key);  
      System.out.println(key+"--"+val);
  } 
  
   Runtime rn = Runtime.getRuntime();
     @SuppressWarnings("unused")
  Process p = null;
     try
     {
      String str = "\"C:\\Program Files\\360\\360safe\\360rpt.exe\"" ;     
     
      p = rn.exec(str);
     }
     catch (Exception e)
     {
      System.out.println("Error to run the exe");
     }

 }
}

java获取系统环境
import java.util.Properties;

public class TestOS {
 public static void main(String[] args) {
  Properties props=System.getProperties(); //系统属性

      System.out.println("Java的运行环境版本:"+props.getProperty("java.version"));
      System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor"));
      System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url"));
      System.out.println("Java的安装路径:"+props.getProperty("java.home"));
      System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version"));
      System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor"));
      System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name"));
      System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version"));
      System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor"));
      System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name"));
      System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version"));
      System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender"));
      System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name"));
      System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version"));
      System.out.println("Java的类路径:"+props.getProperty("java.class.path"));
      System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path"));
      System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir"));
      System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs"));
      System.out.println("操作系统的名称:"+props.getProperty("os.name"));
      System.out.println("操作系统的构架:"+props.getProperty("os.arch"));
      System.out.println("操作系统的版本:"+props.getProperty("os.version"));
      System.out.println("文件分隔符:"+props.getProperty("file.separator"));   //在 unix 系统中是”/”
      System.out.println("路径分隔符:"+props.getProperty("path.separator"));   //在 unix 系统中是”:”
      System.out.println("行分隔符:"+props.getProperty("line.separator"));   //在 unix 系统中是”/n”
      System.out.println("用户的账户名称:"+props.getProperty("user.name"));
      System.out.println("用户的主目录:"+props.getProperty("user.home"));
      System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
 }
 
}

Global site tag (gtag.js) - Google Analytics