专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 几个非常好用的Java程序代码

几个非常好用的Java程序代码

更新时间:2021-05-14 16:11:17 来源:动力节点 浏览974次

1.字符串有整型的相互转换

String a = String.valueOf(2);  //integer to numeric string 
int i = Integer.parseInt(a); //numeric string to an int

2.向文件末尾添加内容

BufferedWriter out = null; 
try { 
  out = new BufferedWriter(new FileWriter(”filename”, true)); 
  out.write(”aString”); 
} catch (IOException e) { 
  // error processing code 
} finally { 
  if (out != null) { 
    out.close(); 
  } 
}

3.得到当前方法的名字

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();

4.转字符串到日期

java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);
或者是:
SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" ); 
Date date = format.parse( myString );

5.使用JDBC链接Oracle

public class OracleJdbcTest 
{ 
  String driverClass = "oracle.jdbc.driver.OracleDriver"; 
 
  Connection con; 
 
  public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException 
  { 
    Properties props = new Properties(); 
    props.load(fs); 
    String url = props.getProperty("db.url"); 
    String userName = props.getProperty("db.user"); 
    String password = props.getProperty("db.password"); 
    Class.forName(driverClass); 
 
    con=DriverManager.getConnection(url, userName, password); 
  } 
 
  public void fetch() throws SQLException, IOException 
  { 
    PreparedStatement ps = con.prepareStatement("select SYSDATE from dual"); 
    ResultSet rs = ps.executeQuery(); 
 
    while (rs.next()) 
    { 
      // do the thing you do 
    } 
    rs.close(); 
    ps.close(); 
  } 
 
  public static void main(String[] args) 
  { 
    OracleJdbcTest test = new OracleJdbcTest(); 
    test.init(); 
    test.fetch(); 
  } 
}

以上就是动力节点小编介绍的"几个非常好用的Java程序代码",希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>