Why is the Google App Engine JDBC connection URL not working? - java

I am developing a Website with a Registration page. I want to store the user details in the table of the MySQL database on Google App Engine. Following is the JDBC connection URL:-
Connection con = DriverManager.getConnection("jdbc:mysql://google/my_database_name?cloudSqlInstance=my_db_instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=My_Username&password=My_Password&useSSL=false", "root", "My_Password");
But this URL isn't working as even after registering, no details are stored in the table of my database. So, do suggest any alternatives. Thank you
The following is the RegistrationServlet code which takes input values from Register.jsp and I want it to store the values into my gcloud db table:-
#WebServlet("/RegistrationServlet")
public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1 L;
public RegistrationServlet() {
super();
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String first_name = request.getParameter("f1");
String last_name = request.getParameter("l1");
String phone = request.getParameter("p1");
String email = request.getParameter("e1");
String uname = request.getParameter("uname");
String user_dob = request.getParameter("udate");
String password = request.getParameter("pwd");
UserData ud = new UserData();
ud.setFirst_name(first_name);
ud.setLast_name(last_name);
ud.setUname(uname);
ud.setPhone(phone);
ud.setEmail(email);
ud.setUser_dob(user_dob);
ud.setPassword(password);
// validate given input
if (first_name.isEmpty() || last_name.isEmpty() || phone.isEmpty() || email.isEmpty() || password.isEmpty()) {
RequestDispatcher rd = request.getRequestDispatcher("Register.jsp");
out.println("<font color=red>Please fill all the fields</font>");
rd.include(request, response);
} else {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// loads mysql driver
try {
Connection con = DriverManager.getConnection("jdbc:mysql://google/my_database_name?cloudSqlInstance=my_db_instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=My_Username&password=My_Password&useSSL=false", "root", "My_Password");
String query = "insert into users values(?,?,?,?,?,?,?)";
PreparedStatement ps = con.prepareStatement(query); //generates sql query
ps.setString(1, first_name);
ps.setString(2, last_name);
ps.setString(3, uname);
ps.setString(4, phone);
ps.setString(5, email);
ps.setString(6, user_dob);
ps.setString(7, password);
ps.executeUpdate();
System.out.println("successfully inserted");
ps.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpSession session = request.getSession();
session.setAttribute("username", first_name);
response.sendRedirect("Success.jsp");
}
}

I've followed the GCP CloudSQL for MySQL connection example and it has worked after deployment. I'll go step by step of the process I did so you can compare and see if there's some configuration step missing on your end.
Used a MySQL 2nd Gen Cloud SQL instance. This instance is in the same project as the Google App Engine I'll deploy the script in
Created a new database in the CloudSQL instance
Ran the command gcloud sql instances describe instance1 to get the instance connection name in the form of <MY-PROJECT>:<REGION>:<INSTANCE-NAME>
Downloaded the GCP Java code samples and got to the Java 8 App Engine standard Cloud SQL example:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/appengine-java8/cloudsql/
Completed the fields <INSTANCE_CONNECTION_NAME>, <user>, <password> and <database> from the POM file with the connection values for my case. This fields will be used by the appengine-web.xml to create the cloudsql property that will be used as the connection URL by the code in CloudSqlServlet.java
Also in the appengine-web.xml, I changed the <service> name to default. There's actually no need to change anything on this file but I chose to change the service due to personal preference
The other required dependencies where already in the downloaded POM
Deployed to GAE with mvn appengine:deploy
This is a minimal example of a working GAE to Cloud SQL connection for MySQL. Try to follow this steps to test if the connection works poperly with the provided code example.

Related

Java JDBC Communications link failure after deploying online

I have created my first API using java httpServlet on netbeans. After days working I could finally connect it to this mySQL googleCloud database and get results locally. But once this API is published on google cloud using appengine, below error was thrown:
Error sqlException:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure The last packet sent successfully to the server was 0
milliseconds ago. The driver has not received any packets from the server.
But the mySQL database is allowing access for all IPs, so what could be the problem? Below is my code:
public class HelloServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
//out.println("Dear vistor, you are accessing my first project deployed on cloud. Thank you");
try {
Class.forName("com.mysql.jdbc.Driver");
String url = String.format("jdbc:mysql://IP:3306/DatabaseName?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
Connection connection = DriverManager.getConnection(url, "userName", "Password");
try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("select test from tableTest;");
while (resultSet.next()) {
out.println(resultSet.getString(1));
}
}
} catch (ClassNotFoundException ex) {
out.println("Error ClassNotFoundException:" + ex.toString());
} catch (SQLException ex) {
out.println("Error sqlException:" + ex.toString());
}
catch(Exception ex){
out.println("Exception:" + ex.toString());
}
}
}
Update:
Based on this link, I have discovered that when publishing online I should not use a password. So I have updated my code to use jdbc google drive in this way:
Class.forName("com.mysql.jdbc.GoogleDriver");
String url = String.format("jdbc:google:mysql://appId:instanceId/dbName?user=root");
Connection connection = DriverManager.getConnection(url);
But when I published online , same error was thrown...
Update2:
Could it be bcause my appId contains "-" like "test-123"? But actually I am sending my url in this form:
url = "jdbc:google:mysql://test-123:myInstance/myDatabase?user=root&useUnicode=true&characterEncoding=UTF-8";

JDBC, connection is ok but does not execute query

I'm writing a small program with a mysql connection. I have to insert data into the database. The connection is ok but when I try to execute a query it doesn't work. all statements after executeQuery() statement doesn't work.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Connection connect=null;
PreparedStatement preparedStatement;
String JDBC_DRIVER="com.mysql.jdbc.Driver";
String DB_URL="jdbc:mysql//localhost/jarvis";
String USER = "test";
String PASS = "test";
try{
Class.forName(JDBC_DRIVER);
connect = DriverManager.getConnection(DB_URL, USER, PASS);
}
catch(ClassNotFoundException e){
out.println("Errore: "+e);
} catch (SQLException ex) {
Logger.getLogger(Prova.class.getName()).log(Level.SEVERE, null, ex);
}
out.println();
out.println("qui");
String query = "INSERT INTO users(Nome, Cognome, Username, Password) values(?, ?, ?, ?)";
try (PreparedStatement insert = connect.prepareStatement(query)) {
insert.setString(1, "name");
insert.setString(2, "sur");
insert.setString(3, "gvhgv");
insert.setString(4, "qfwe");
insert.executeUpdate();
}
catch(Exception e){
out.println("Errore "+e);
}
out.println("Fine");
}
on executeQuery it stop working and doesn't insert the values into the database.
P.S.: sorry for my English
foto
Firstly your JDBC Connection URL is wrong, it should be this
String DB_URL = jdbc:mysql://localhost/jarvis
instead of this:
String DB_URL = jdbc:mysql//localhost/jarvis
You can follow the MySQL documentation whenever you are in doubt about anything.
Please note that mentioning port is not necessary if it is skipped it would default to MySQL's default port 3306.
Secondly, use executeUpdate() method instead of executeQuery method. It is best suggested to test your code in chunks, like DB Connection is successful, able to retrieve data from the underlying DB and then inserting into the DB.
Or even a better way is to debug your code and at least provide where you find the NullPointerException as you say you're getting now!
Edit:
You must have your JDBC Connector/J jar available for your code to access it if you use an IDE (then in its build path) and in the CLASSPATH global variable (for references when we don't use an IDE). It is better to have it on your CLASSPATH if you are not going to change these dependencies any often.
Hope this helps!

How to crawl a website after login in it with username and password

I have written a webcrawler that crawls a website with keyward but i want to login to my specified website and filter information by keyword.How to achive that. i posting my code so far i have done .
public class DB {
public Connection conn = null;
public DB() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
conn = DriverManager.getConnection(url, "root","root");
System.out.println("conn built");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public ResultSet runSql(String sql) throws SQLException {
Statement sta = conn.createStatement();
return sta.executeQuery(sql);
}
public boolean runSql2(String sql) throws SQLException {
Statement sta = conn.createStatement();
return sta.execute(sql);
}
#Override
protected void finalize() throws Throwable {
if (conn != null || !conn.isClosed()) {
conn.close();
}
}
}
public class Main {
public static DB db = new DB();
public static void main(String[] args) throws SQLException, IOException {
db.runSql2("TRUNCATE Record;");
processPage("http://m.naukri.com/login");
}
public static void processPage(String URL) throws SQLException, IOException{
//check if the given URL is already in database;
String sql = "select * from Record where URL = '"+URL+"'";
ResultSet rs = db.runSql(sql);
if(rs.next()){
}else{
//store the URL to database to avoid parsing again
sql = "INSERT INTO `test`.`Record` " + "(`URL`) VALUES " + "(?);";
PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, URL);
stmt.execute();
//get useful information
Connection.Response res = Jsoup.connect("http://www.naukri.com/").data("username","jeet.chatterjee.88#gmail.com","password","Letmein321")
.method(Method.POST)
.execute();
//http://m.naukri.com/login
Map<String, String> loginCookies = res.cookies();
Document doc = Jsoup.connect("http://m.naukri.com/login")
.cookies(loginCookies)
.get();
if(doc.text().contains("")){
System.out.println(URL);
}
//get all links and recursively call the processPage method
Elements questions = doc.select("a[href]");
for(Element link: questions){
if(link.attr("abs:href").contains("naukri.com"))
processPage(link.attr("abs:href"));
}
}
}
}
And the table structure also
CREATE TABLE IF NOT EXISTS `Record` (
`RecordID` INT(11) NOT NULL AUTO_INCREMENT,
`URL` text NOT NULL,
PRIMARY KEY (`RecordID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Now i want to use my username and password for that crawling so that crawler can log in to the site dynamically and crawl infomation on the basis of keyword..
Lets say my username is lucifer & password is lucifer123
your approach is for stateless web access, which usually works for web services. sites are stateful. you authenticate once and after that, they use the session key stored in your cookie to authenticate you ( other means of authentication is also possible), so it is required. you must send parameters that your browser is sending. try monitoring what your browser send to site with firebug, and reproduce that in your code
--update--
Jsoup.connect("url")
.cookie("cookie-name", "cookie-value")
.header("header-name", "header-value")
.data("data-name","data-value");
u can add multi cookie | header | data. and there is function for adding values from Map.
to find out what must be set, add fire bug to your browser, they all have their default developer console which can be started with F12. go to the url u want to get data and just add all thing in there to your jsoup request.
i added some images from your site result
i marked important part in red.
u can get required cookies in your code with sending these info to site and get cookie from that and after getting response.cookies you attach these cookies to every request u make ;)
p.s: change your password A.S.A.P

servlet netbeans mysql connect

I am new to sql and I am using netbeans and mysql.
I am trying to make a servlet that will check login details against a database.
I found some code oline but it doesn't interact with the database. Also every example I find doesn't work either. What am I doing wrong.
Here is some of the code
package login;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
*
* #author john
*/
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
if (Validate.CheckUser(userName)) {
RequestDispatcher rs = request.getRequestDispatcher("Welcome");
rs.forward(request,
response
);
}
else
{
RequestDispatcher rs = request.getRequestDispatcher("index.html");
rs.include(request , response);
}
}
}
AND
package login;
import java.sql.*;
/**
*
* #author john
*/
public class Validate {
public static boolean CheckUser(String userName) {
boolean st = false;
try {
Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase?zeroDateTimeBehavior=convertToNull", "root", "admin");
PreparedStatement ps = con.prepareStatement("SELECT userName FROM userData where userName=?");
ps.setString(1, userName);
ResultSet rs = ps.executeQuery();
st = rs.next();
} catch (Exception e) {
e.printStackTrace();
}
return st;
}
}
What are the extra steps I need to take to get this to connect to the data base. I am using the latest version of netbeans.
I have already put the 5.1.26-bin.jar file into the library.
Mynewdatabase is running just fine(I connected using a connection pool a JSP before in a different application).
This application will not connect to the data base though. I am not using a connection pool here.
Any help would be great.
Connecting to a database has nothing at all do with NetBeans or servlets. I'd advise that you get the database class working before adding in unnecessary complications.
It'd help if you'd post an error. "Not working" isn't helpful.
Your driver class for MySQL doesn't look correct to me. Should be com.mysql.jdbc.Driver.
But there's lots more wrong with your code than that.
Your query brings back a username. No password? Not useful.
No application should access a database using the root admin password. Create an ID for the app and only GRANT sufficient permission to perform its tasks.
You don't close resources in your method. You'll have problems if you ever get this to run.
Come back if you get it to work at all.
To retrieve from database you should use this way
while(rs.next())
{
String coulm1=rs.getString(1);
String column2=rs.getString(2);
}
The code should be like this:
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase", "root", "admin");
You will also need to add mysql driver jar file to the classpath. Firstly try to connect with database with simple parameters in database url, then move to the complex ones when you are fully ready!

Java to mysql jdbc connection problems

I'm new with getting a java applet to connect to a mysql database. This is my first time attempting to do so.
I've researched a lot and have seen lots of tutorials, but I'm still facing problems.
Since my java applet goes to a browser, I did sign it.
I've also been using jdbc and the jar file has been imported properly to my library.
I've also been using 000webhost.com and have been trying to connect to the database from both the IDE and the browser.
I also got two of my friends to help me. One of them had to go early and the other don't know where I went wrong.
Here is my codes:
http://prntscr.com/oagfi
I've come to the conclusion that the DriverManager.getConnection(...) is whats giving me problems.
The error reads...
http://prntscr.com/oaetz
I have also tried looking up the cause but still no luck.
Is there anything I can do to fix this problem? I'm curious of what this cause mean and why I'm having trouble.
If you are on free 000webhost accounts than you cant access your database outside your host account.
Check the version of JDBC Connector you are using. Also following link will help you to do JDBC Connection -
Connect Java to a MySQL database
Are you behind a firewall/proxy server? If so, does it permit outgoing connections on port 3306 you're using? This was a problem I had once, our corporate firewall was so crippled we could only talk out via the http /https ports.
From Applet (if my memory not fail), you must use Signed code and/or you can only connect to remotehost from Applet was downloaded... if not, security exceptions are throwed. (applets run on a limited/restricted sandbox)
/* RegistrationDAO*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.tcs.ignite.connectionname.DAO;
import com.tcs.ignite.connectionname.bean.Register;
import com.tcs.ignite.connectionname.util.Eyeconnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class RegisterDAO {
private Connection connection;
private connectionnamecon;
public RegisterDAO() {
con = new connectionname();
}
public boolean insertRecords(Register rg) throws Exception {
connection = con.openConnection();
String select="select * from register";
PreparedStatement ps=connection.prepareStatement(select);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
String email=rs.getString("user_email");
if(rg.getUser_email().equals(email))
{
return false;
}
}
ps.close();
String query = "insert into register(user_Fname,user_Lname,password,confirm_pass,contact_no,user_email,user_address,user_pincode) VALUES (?,?,?,?,?,?,?,?)";
ps = connection.prepareStatement(query);
ps.setString(1, rg.getUser_Fname());
ps.setString(2, rg.getUser_Lname());
ps.setString(3, rg.getPassword());
ps.setString(4, rg.getConfirm_pass());
ps.setString(5, rg.getContact_no());
ps.setString(6, rg.getUser_email());
ps.setString(7, rg.getUser_address());
ps.setString(8,rg.getUser_pincode());
int rowcount = ps.executeUpdate();
con.closeConnection();
if (rowcount == 0) {
return false;
} else {
return true;
}
}
}
/*
RegistrationManager*/
*/
package com.tcs.ignite.connectionname.Manager;
import com.tcs.ignite.connectionname.DAO.RegisterDAO;
import com.tcs.ignite.connectionname.bean.Register;
public class RegisterManager {
public boolean insertManager(Register rg) throws Exception {
RegisterDAO regdao = new RegisterDAO();
boolean result = regdao.insertRecords(rg);
if(result==true)
{
return true;
}
else
{
return false;
}
}
}
/*RegistrationServlet*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Register reg=new Register();
reg.setUser_Fname(request.getParameter("firstname"));
reg.setUser_Lname(request.getParameter("lastname"));
reg.setPassword(request.getParameter("password"));
reg.setConfirm_pass(request.getParameter("confirm_password"));
reg.setContact_no(request.getParameter("mobile"));
reg.setUser_email(request.getParameter("email"));
reg.setUser_address(request.getParameter("address"));
reg.setUser_pincode(request.getParameter("pincode"));
RegisterManager regManager=new RegisterManager();
if(regManager.insertManager(reg)){
// RequestDispatcher requestDispatcher= request.getRequestDispatcher("TCSBLUE.jsp");
// requestDispatcher.forward(request, response);
HttpSession session = request.getSession(true);
session.setAttribute("loginid", reg.getUser_email());
//out.print(session.getAttribute("loginid"));
out.write("Successfully Registered...");
}
else
{
// RequestDispatcher requestDispatcher= request.getRequestDispatcher("Error.jsp");
// requestDispatcher.forward(request, response);
out.write("Something is going wrong....");
}
}
catch(Exception ex)
{
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
}
finally {
out.close();
}
}

Categories