如何从Java应用程序连接到在线MySQL数据库

时间:2022-06-23 02:59:21

For an assignment I am developing an application that need to perform insertions in a MySQL database. I am using JDBC but the hosting services I have don't provide support for JDBC.

我正在开发一个应用程序,需要在MySQL数据库中执行插入操作。我正在使用JDBC,但是我没有提供对JDBC的支持。

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

我能做什么?这个方法太旧了吗?你能给我推荐一些可以从Java应用程序直接插入到远程MySQL数据库的方法吗?

EDIT: 如何从Java应用程序连接到在线MySQL数据库

编辑:

I wonder why people spend time editing old questions in a useless way... do you really care if I write mysql or MySQL? Does not make sense to me, you could spend your time actually answering the question...

我想知道为什么人们花时间用无用的方式编辑老问题……你真的在乎我写的是mysql还是mysql ?对我来说没有意义,你可以花时间回答这个问题……

1 个解决方案

#1


3  

No, it's not old, in order to connect to a database using java you need to have JDBC! start a simple JSP proof-of-concept in order to do that and probably you'll see it working. I don't think there's a host without JDBC support. Anyways, here is a really simple / insecure / old fashioned JDBC example:

不,它不是旧的,为了连接到一个使用java的数据库你需要有JDBC!启动一个简单的JSP概念验证,以实现这一点,您可能会看到它正在工作。我不认为有一个没有JDBC支持的主机。无论如何,这里有一个非常简单/不安全/老式的JDBC示例:

<%@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 proof-of-concept, this code is vulnerable to SQL injection and/or needs to be improved.

编辑:请不要认为这是概念验证,这段代码容易受到SQL注入的影响,或者需要改进。

#1


3  

No, it's not old, in order to connect to a database using java you need to have JDBC! start a simple JSP proof-of-concept in order to do that and probably you'll see it working. I don't think there's a host without JDBC support. Anyways, here is a really simple / insecure / old fashioned JDBC example:

不,它不是旧的,为了连接到一个使用java的数据库你需要有JDBC!启动一个简单的JSP概念验证,以实现这一点,您可能会看到它正在工作。我不认为有一个没有JDBC支持的主机。无论如何,这里有一个非常简单/不安全/老式的JDBC示例:

<%@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 proof-of-concept, this code is vulnerable to SQL injection and/or needs to be improved.

编辑:请不要认为这是概念验证,这段代码容易受到SQL注入的影响,或者需要改进。