public static void main(String[] args) throws Exception{
String driver = "oracle.jdbc.driver.OracleDriver";
//localhost指本机,也可以用本地ip地址代替,3306为MySQL数据库的默认端口号,“user”为要连接的数据库名
String url = "jdbc:oracle:thin:@ip:端口号/服务器";
//填入数据库的用户名跟密码
String username = "帐号";
String password = "密码";
try{
Class.forName(driver);//加载驱动程序,此处运用隐式注册驱动程序的方法
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try{
Connection con = DriverManager.getConnection(url,username,password);//创建连接对象
Statement st = con.createStatement();//创建sql执行对象
//查询
ResultSet rs=st.executeQuery("select * from user");
while(rs.next()){
String name=rs.getString(2);
//System.out.println("{header: "",field: "",align:"center", headerAlign: "center"},);
//字段多了,并且代码格式一样,也可以生成java代码
System.out.print("nvl(sum(ta."+name+"_Q),0) as "+name+",");
}
//操作
int num=st.executeUpdate("delete from user");
System.out.println(num);
if(rs!=null){
try {
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(con !=null){
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
catch(SQLException e){
e.printStackTrace();
}
}