How to connect to online MySQL database from Java application

For the assignment, I am developing an application that needs to do inserts on a MySQL database. I am using JDBC, but the hosting services that I do not have do not support JDBC.

What can I do? Is this method too old? Can you please suggest me any alternative to basically paste from Java application to remote MySQL database?

EDIT :enter image description here

I wonder why people are wasting time editing old questions in a useless way ... do you really care if I write mysql or MySQL? Doesn't make sense to me, you could waste your time actually answering the question ...

+3


source to share


1 answer


No, it is not old, to connect to the database using java you need to have JDBC! run a simple JSP proof of concept to do this and you can probably see it working. I don't think there is a host without JDBC support. Anyway, here's a very basic / unsafe / old fashioned JDBC example:

<%@page import="java.sql.*"%>
<%
try {
String name=request.getParameter("name");
String address=request.getParameter("address");
Class.forName("com.mysql.jdbc.Driver");
           Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdb", youruser, yourpassword);
           Statement st=cn.createStatement();
           int i = st.executeUpdate("insert into user(name,address) values('"+name+"','"+address+"')");
           out.println("Data is inserted successfully");
           st.close();
           cn.close();
} catch (Exception e) {
   out.println(e.getMessage());
}
%>

      



EDIT: Please not that this is a proof-of-concept, this code is vulnerable to SQL injection and / or needs improvement.

+3


source







All Articles