parent
71ada9ab87
commit
40f35c9723
@ -0,0 +1,102 @@
|
|||||||
|
package com.demo.files;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.sql.Blob;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.apache.commons.fileupload.FileItem;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import com.domain.GetDBConnection;
|
||||||
|
import com.domain.musicMIDI;
|
||||||
|
|
||||||
|
import net.sf.json.JSONObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Servlet implementation class QueyOneFilenames
|
||||||
|
*/
|
||||||
|
@WebServlet("/QueyOneFilenames")
|
||||||
|
public class QueyOneFilenames extends HttpServlet {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see HttpServlet#HttpServlet()
|
||||||
|
*/
|
||||||
|
public QueyOneFilenames() {
|
||||||
|
super();
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
*/
|
||||||
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Connection con=null;
|
||||||
|
Statement sql;
|
||||||
|
ResultSet rs;
|
||||||
|
PreparedStatement preSql;
|
||||||
|
con = GetDBConnection.connectDB("musicMySQL","root","root");
|
||||||
|
if(con == null ) return;
|
||||||
|
try {
|
||||||
|
sql=con.createStatement();
|
||||||
|
con.setAutoCommit(false);//程序必须调用commit或者rollback方法
|
||||||
|
String sqlStr ="SELECT filename FROM music WHERE username=?";
|
||||||
|
preSql = con.prepareStatement(sqlStr);
|
||||||
|
String username = request.getParameter("username");
|
||||||
|
username = new String(username.getBytes("iso8859-1"),"UTF-8");
|
||||||
|
System.out.println(username);
|
||||||
|
preSql.setString(1, username);
|
||||||
|
rs=preSql.executeQuery();
|
||||||
|
int i=0;
|
||||||
|
|
||||||
|
ArrayList<musicMIDI> musiclist = new ArrayList<>();
|
||||||
|
while(rs.next()) {
|
||||||
|
musicMIDI music = new musicMIDI();
|
||||||
|
music.setFilename(rs.getString("filename"));
|
||||||
|
System.out.println(music.getFilename());
|
||||||
|
musiclist.add(music);
|
||||||
|
}
|
||||||
|
PrintWriter out = response.getWriter();
|
||||||
|
String json2[] = null;
|
||||||
|
JSONObject jsonobject = new JSONObject();
|
||||||
|
jsonobject.put("size", musiclist.size());
|
||||||
|
for(i=0;i<musiclist.size();i++) {
|
||||||
|
musicMIDI item = musiclist.get(i);//循环遍历list
|
||||||
|
jsonobject.put("filename"+i, item.getFilename());
|
||||||
|
System.out.println(item.getFilename());
|
||||||
|
System.out.println(jsonobject);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(jsonobject);
|
||||||
|
out.println(jsonobject);
|
||||||
|
|
||||||
|
}catch(SQLException e) {
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue