java如何用sql语句求平均值
已经连接上了数据库,但不知道用哪个类来执行sql语句。ConnectionEx1Con=null;StatementEx1Stmt=null;try{Class.forN...
已经连接上了数据库,但不知道用哪个类来执行sql语句。
Connection Ex1Con=null;
Statement Ex1Stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Ex1Con = DriverManager.getConnection("jdbc:odbc:diago", "", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Ex1Stmt = Ex1Con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
StdutempName = rs.getString("StudName");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
rs = Ex1Stmt.executeQuery("select avg(English) from studbasicinfo");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(rs.next())
{
System.out.println(rs.getString("English"));
};
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 展开
Connection Ex1Con=null;
Statement Ex1Stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Ex1Con = DriverManager.getConnection("jdbc:odbc:diago", "", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Ex1Stmt = Ex1Con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
StdutempName = rs.getString("StudName");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
rs = Ex1Stmt.executeQuery("select avg(English) from studbasicinfo");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while(rs.next())
{
System.out.println(rs.getString("English"));
};
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 展开
2个回答
展开全部
一种是把数据全读出来,然后求平均值,二是直接用sql语句求平均值,显然,第二种要好
追问
第二种怎么弄啊
追答
你有学过sql查询语句吗?
如果给出String sql="select avg(grade) from table";类似的字符串,执行这条语句即可
看你上面的代码,感觉在用odbc连接数据库,怎么不用Jdbc呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-23
展开全部
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott","tiger");
Statement st = conn.createStatement();
ResultSet set = st.executeQuery(sql);//返回结果集
st.execute(sql);//返回boolean类型,是否成功。
Statement st = conn.createStatement();
ResultSet set = st.executeQuery(sql);//返回结果集
st.execute(sql);//返回boolean类型,是否成功。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询