-------------------------查询连接``。
package DBcon;
import java.sql.*;
import java.util.ArrayList;
public class Insert {
private static CreateDBcon con=new CreateDBcon();
private static Connection dbconn=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
public Insert() {
}
public void pst(String sql){
try {
dbconn=con.createDBcon();
ps=dbconn.prepareStatement(sql);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertOneTitle(String onetitle){
try {
String sql="insert into oneTitle(titleName ) values('"+onetitle+"')";
this.pst(sql);
ps.executeUpdate();
this.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public ArrayList add(){
ArrayList add=new ArrayList();
return add;
}
public void SelectOneTitle(){
try {
String sql="Select titleName from oneTitle";
this.pst(sql);
rs=ps.executeQuery();
while (rs.next()) {
this.add().add(rs.getString(1));
}
} catch (Exception ex) {
}
}
private void close(){
try {
if (ps!=null) {
ps.close();
}
if (dbconn!=null) {
dbconn.close();
}
} catch (Exception ex) {
}
}
}
------------------取的值``````````
package TitleServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import DBcon.Insert;
public class TwoTitleServlet extends HttpServlet {
private static Insert select=new Insert();
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
ArrayList add=select.add();
if (add.size()>0) {
int i=0;
for (; i <add.size(); i++) {
add.get(i);
}
session.setAttribute("oneTitle",add.get(i));
} else {
this.outErrMessage(out);
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void outErrMessage(PrintWriter out){
out.print("<SCRIPT LANGUAGE='JavaScript'>");
out.print("alert('没有一级标题!')</SCRIPT>");
out.close();
}
//Clean up resources
package DBcon;
import java.sql.*;
import java.util.ArrayList;
public class Insert {
private static CreateDBcon con=new CreateDBcon();
private static Connection dbconn=null;
private static PreparedStatement ps=null;
private static ResultSet rs=null;
public Insert() {
}
public void pst(String sql){
try {
dbconn=con.createDBcon();
ps=dbconn.prepareStatement(sql);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insertOneTitle(String onetitle){
try {
String sql="insert into oneTitle(titleName ) values('"+onetitle+"')";
this.pst(sql);
ps.executeUpdate();
this.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public ArrayList add(){
ArrayList add=new ArrayList();
return add;
}
public void SelectOneTitle(){
try {
String sql="Select titleName from oneTitle";
this.pst(sql);
rs=ps.executeQuery();
while (rs.next()) {
this.add().add(rs.getString(1));
}
} catch (Exception ex) {
}
}
private void close(){
try {
if (ps!=null) {
ps.close();
}
if (dbconn!=null) {
dbconn.close();
}
} catch (Exception ex) {
}
}
}
------------------取的值``````````
package TitleServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import DBcon.Insert;
public class TwoTitleServlet extends HttpServlet {
private static Insert select=new Insert();
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
ArrayList add=select.add();
if (add.size()>0) {
int i=0;
for (; i <add.size(); i++) {
add.get(i);
}
session.setAttribute("oneTitle",add.get(i));
} else {
this.outErrMessage(out);
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void outErrMessage(PrintWriter out){
out.print("<SCRIPT LANGUAGE='JavaScript'>");
out.print("alert('没有一级标题!')</SCRIPT>");
out.close();
}
//Clean up resources