JDBC连接后的HTTPS响应

时间:2015-01-27 09:47:45

标签: java http jdbc

我正在向网络服务器发送请求

Test1.con1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0"); 
Test1.con1.setRequestMethod("POST");
Test1.con1.setDoInput(true); 
BufferedReader br = new BufferedReader(new InputStreamReader(Test1.con1.getInputStream()));
cookie  =  Test1.con1.getHeaderField(6) + ";" +  cookie;

JDBC代码

String url = "jdbc:mysql://XXXXXXXX:XXXX";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"XXXXX","XXXX");
System.out.println ("Database connection established");
String myTableName = "CREATE TABLE EMPTABLE (" 
        + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
        + "batch INT(64), "
        + "PRIMARY KEY(idNo))";  
try {
    statement = conn.createStatement();
    //This line has the issue
    statement.executeUpdate(myTableName);
    System.out.println("Table Created");
}

如果我在建立与远程服务器的JDBC连接之前使用此代码,我会收到cookie,但我的要求是在JDBC连接后发送请求。任何人都可以建议我怎么做?

如果我使用像

这样的代码
Test1.con1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0"); 
Test1.con1.setRequestMethod("POST");
Test1.con1.setDoInput(true); 
BufferedReader br = new BufferedReader(new InputStreamReader(Test1.con1.getInputStream()));
cookie  =  Test1.con1.getHeaderField(6) + ";" +  cookie; /*(" I am able to get cookie)*/

String url = "jdbc:mysql://XXXXXXXX:XXXX";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"XXXXX","XXXX");
System.out.println ("Database connection established");
String myTableName = "CREATE TABLE EMPTABLE (" 
        + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
        + "batch INT(64), "
        + "PRIMARY KEY(idNo))";  
try {
    statement = conn.createStatement();
    //This line has the issue
    statement.executeUpdate(myTableName);
    System.out.println("Table Created");
}

在这种情况下,我能够获得cookie值但是如果我使用这样的代码

String url = "jdbc:mysql://XXXXXXXX:XXXX";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"XXXXX","XXXX");
System.out.println ("Database connection established");
String myTableName = "CREATE TABLE EMPTABLE (" 
        + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
        + "batch INT(64), "
        + "PRIMARY KEY(idNo))";  
try {
    statement = conn.createStatement();
    //This line has the issue
    statement.executeUpdate(myTableName);
    System.out.println("Table Created");
}
Test1.con1.setRequestMethod("POST");
Test1.con1.setDoInput(true); 
BufferedReader br = new BufferedReader(new  
InputStreamReader(Test1.con1.getInputStream()));   
cookie  =  Test1.con1.getHeaderField(6) + ";" +  cookie;

这里无法获取cookie

1 个答案:

答案 0 :(得分:0)

您的数据库连接代码可能会引发异常,并且无法访问您的Cookie请求代码。

在db连接之间添加try catch并打印stacktrace。这样你就可以找出确切的原因。

 try {
  String url = "jdbc:mysql://XXXXXXXX:XXXX";
  Class.forName ("com.mysql.jdbc.Driver");
  conn = DriverManager.getConnection (url,"XXXXX","XXXX");
   System.out.println ("Database connection established");
     String myTableName = "CREATE TABLE EMPTABLE (" 
    + "idNo INT(64) NOT NULL AUTO_INCREMENT,"  
    + "batch INT(64), "
    + "PRIMARY KEY(idNo))";  

     statement = conn.createStatement();
    //This line has the issue
    statement.executeUpdate(myTableName);
    System.out.println("Table Created");
 }catch(Exception e){
     e.printStackTrace()
 }