81,092
社区成员
发帖
与我相关
我的任务
分享
package com.drp.jdbc.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class DrpConnectionTest {
public static void main(String[] args){
init();
}
private static final String drpconfig="/drpconfig.properties";
private static Map<String, String> configMap = null;
private static DrpConnectionTest drpconn = new DrpConnectionTest();
public static void DrpConnectionTest1(){
init();
initDriver();
}
private static void init(){
configMap = new HashMap<String, String>();
Properties ps = new Properties();
try {
ps.load(ps.getClass().getResourceAsStream(drpconfig)); // 这就是第32行。??????
Set<Object> keys = ps.keySet();
for(Object key : keys){
String value = (String)ps.get(key);
configMap.put((String)key, value.trim());
System.out.println("key "+ key +", value " + value);
System.out.println("aaa "+configMap.get("drp.driver_class"));
}
} catch (IOException e) {
System.out.println(e);
}
}
private static void initDriver(){
try {
Class.forName(configMap.get("drp.driver_class")).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
init(); // 这是第56行。?????????????????
return DriverManager.getConnection(configMap.get("drp.url"),configMap.get("drp.username"),configMap.get("drp.password"));
}
public static void closePreparedStatement(PreparedStatement pstmt){
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
pstmt = null;
}
public static void closeConnection(Connection conn){
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
conn = null;
}
public static void closeResultSet(ResultSet rs){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
rs = null;
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.drp.jdbc.util.*;
public class user_add_servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String sql = "insert into ";
int result = 0;
Connection conn = null;
PreparedStatement pstmt= null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DrpConnectionTest.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(0, request.getParameter("userId"));
pstmt.setString(1, request.getParameter("userName"));
pstmt.setString(2, request.getParameter("password"));
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(result == 0){
out.println("添加不成功");
} else {
out.println("添加成功1111111111111");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}