2013-02-25 286 views
1
  • 我有下面的servlet代码。
  • 在控制台中打印下面的行时,应该以红色打印。 “*连接名称”+ cname +“已存在,请用另一个名字尝试”。如何得到这个。

我的servlet代码:使用java设置字符串颜色

try{ 
      Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 
      con =DriverManager.getConnection("jdbc:oracle:thin:@"+host+":"+port+"/"+service,username,password); 
      con.setAutoCommit(false); 
      if(con!=null){ 
       if(key == null){ 
        out.println("Connected Successfully"); 
        String rootPath=request.getSession().getServletContext().getRealPath("/"); 
        System.out.println(rootPath); 
        String propPath=rootPath+"/WEB-INF/"; 
        PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(propPath+"importedDB.properties", true))); 
        out1.println(cname+"=jdbc:oracle:thin:@"+host+","+port+","+service+","+username+","+password); 
        out1.close(); 
       } 
       else{     
        out.println("*Connection name "+cname+" already exists. Please try with another name"); 
       } 
      }else{ 
       out.println("Error in getting connection"); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
+0

明白了......谢谢 – Rachel 2013-02-25 08:17:57

+0

您正在编写代码来访问数据库并在'servlet'中编写一些控制台文本! – Apurv 2013-02-25 08:48:09

+0

@Apurv问题不在于谁教她的Java,是谁教她的网络编程,因为在问题(和代码中)中存在许多错误观念**。 – 2013-02-25 08:49:31

回答