我制作了一个模拟器,其中,通过文件读取器数据从文本文件中读取并将每行插入数据库中作为记录。也可以通过jsp页面中的select查询成功查看数据。现在我想根据严重程度显示不同颜色的每一行。如果我在mysql表中手动插入记录则没有问题。 bt如果数据由模拟器插入那么数据甚至不允许comapare与字符串... PLZ PLZ帮助我..
代码:
package file_mgt;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class File_mgt
*/
@WebServlet("/File_mgt")
public class File_mgt extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public File_mgt() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setIntHeader("Refresh", 3);
PrintWriter out=response.getWriter();
String alarm_name="";
String alarm_severity="";
String alarm_desc="";
Date time;
BufferedReader br = null;
Random rand = new Random();
int Tlines=0;
int minimum=1;
int maximum=10;
String sCurrentLine = null;
String line1 = null;
int counter=0;
ArrayList<String> x = new ArrayList<String>();
Connection conn=null;
PreparedStatement pst=null;
try {
response.setIntHeader("Refresh", 7);
Class.forName("com.mysql.jdbc.Driver");
//open connection
conn= DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","root");
out.println("successfully connected!!!! :");
out.println("hi");
//String fileName = "D:/workSpace/Filemgt/src/files/test.txt";
br = new BufferedReader(new FileReader("D:/workSpace/Filemgt/src/files/test.txt"));
//========== total numbers of lines in file
while (br.readLine() != null) {
Tlines++;
}
out.println("total number of lines:"+Tlines);
//================generate random number======
Random rn = new Random();
int range = maximum - minimum + 1;
int randomNum = rn.nextInt(range) + minimum;
out.println("random number:"+randomNum);
//=============
br = new BufferedReader(new FileReader("D:/workSpace/Filemgt/src/files/test.txt"));
//line1 = br.readLine();
//out.println(line1);
while ((line1 = br.readLine()) != null) {
//out.println(sCurrentLine);
counter++;
if(counter == randomNum){
//out.println(line1);
String[] splited = line1.split(",");
alarm_name=splited[0];
alarm_severity=splited[1];
alarm_desc=splited[2];
Insertion(conn,pst,alarm_name,alarm_severity,alarm_desc);
out.println("\n");
out.println(alarm_name);
out.println(alarm_severity);
out.println(alarm_desc);
}
}
} catch (IOException e) {
e.printStackTrace();
out.println(e);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.println(e);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
out.println(e);
} finally {
if (pst != null) {
try {
pst.close();
} catch (SQLException e) { /* ignored */}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { /* ignored */}
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
public static void Insertion(Connection conn, PreparedStatement pst, String alarm_name , String alarm_severity, String alarm_desc) throws SQLException{
java.util.Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
//pst.setTimestamp(1, timestamp);
pst=conn.prepareStatement("insert into test_alams2(name, severrity, description,Time) values(?,?,?,?)");
pst.setString(1, alarm_name);
pst.setString(2, alarm_severity);
pst.setString(3, alarm_desc);
pst.setTimestamp(4, timestamp);
pst.executeUpdate();
System.out.println("inserted....");
}
}
上面是我的模拟器...通过哪些数据成功插入。
数据检索代码是:
st=conn.createStatement();
String sql="select * from alarms2";
rs=st.executeQuery(sql);
while(rs.next())
{
alarm_id=rs.getInt("id");
alarm_name=rs.getString("Alarm_name");
alarm_severity=rs.getString("Alarm_severity");
alarm_desc=rs.getString("Alarm_desc");
time=rs.getString("Time");
out.println("<tr>");
%>
<td><a href="table_represent.jsp?id=<%= rs.getString("id") %>" ><%= alarm_id %></a></td>
<%
out.println("<td>"+alarm_name+"</td>");
out.println("<td>"+alarm_severity+"</td>");
out.println("<td>"+alarm_desc+"</td>");
out.println("<td>"+time+"</td>");
//out.println("<td>"+"its major"+"</td>");
out.println("</tr>");
// out.println(" <br> ");
//out.println("\n Severity:"+alarm_severity);
/*
if(alarm_severity.equalsIgnoreCase(Critical))
{
out.println(" <br> ");
out.println("<h1>"+"its true"+"</h1>");
}else {
out.println(" <br> ");
out.println(" => its not working");
}
*/
}
}
catch(Exception e){
out.println(e);
}
现在通过javascript想要改变tr的tr背景颜色,代码是:
<script language="javascript">
var tbl = document.getElementById("tbl-1");
if (tbl != null) {
for (var i = 0; i < tbl.rows.length; i++) {
for (var j = 0; j < tbl.rows[i].cells.length; j++){
if(tbl.rows[i].cells[j].innerHTML == "major")
tbl.rows[i].style.backgroundColor = "#fff000";
else if(tbl.rows[i].cells[j].innerHTML == "minor")
tbl.rows[i].style.backgroundColor = "#000fff";
}
}
}else document.writeln("Sorry");
</script>
bt比较有问题...我无法在模拟器插入的记录上做任何事情。有什么办法吗? 请帮助我。
答案 0 :(得分:0)
尝试此而不是“主要”或“次要”
<script language="javascript">
var tbl = document.getElementById("tbl-1");
if (tbl != null) {
for (var i = 0; i < tbl.rows.length; i++) {
if (i%2==0)
tbl.rows[i].style.backgroundColor = "#fff000";
else
tbl.rows[i].style.backgroundColor = "#000fff";
}
else
document.writeln("Sorry");
</script>
希望这有帮助