Java-使用集成的MySQL执行从客户端发送到服务器的查询
我必须根据用户输入从客户端向服务器发送一个字符串,并将该字符串作为MySQL数据库的查询执行。这是我的服务器:Java-使用集成的MySQL执行从客户端发送到服务器的查询,java,mysql,Java,Mysql,我必须根据用户输入从客户端向服务器发送一个字符串,并将该字符串作为MySQL数据库的查询执行。这是我的服务器: public class Server{ private static ServerSocket serverSocket; private static Socket clientSocket; private static BufferedReader bufferedReader; private static String inputLine; private static i
public class Server{
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static String inputLine;
private static int port = 63400;
public static void main(String[] args){
// Wait for client to connect on 63400
try{
System.out.println("Starting the socket server at port:" + port);
serverSocket = new ServerSocket(port);
System.out.println("Waiting for clients...");
clientSocket = serverSocket.accept();
System.out.println("You're now connected to the server.");
//START of DB connection
try{
System.out.println("Trying to connect to MySQL...");
Class.forName("org.gjt.mm.mysql.Driver"); //Load the driver
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/tvschedule", "user", "password"); //Connect
if(conn == null){
System.out.println("MySQL Server is NOT running!");
return;
} else {
System.out.println("Successfully connected to MySQL!");
}
bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Statement stmt = null;
ResultSet resultSet = null;
try{
stmt = (Statement) conn.createStatement();
while((inputLine = bufferedReader.readLine()) != null) {
resultSet = stmt.executeQuery(inputLine);
}
while (resultSet.next()) {
System.out.print("Show Title: "+resultSet.getString("show_title"));
}
} catch(SQLException e){
e.printStackTrace();
}
finally{
try{
if(stmt != null) stmt.close();
if(resultSet != null) resultSet.close();
if(conn != null) conn.close();
} catch(SQLException e2){
e2.printStackTrace();
}
}
} catch (Exception err){System.out.println(err);}
//END of DB connection
} catch(Exception e) {
System.out.println("Some exception: " + e);
}
}
}
这是客户:
public class Client{
private static Socket socket;
private static PrintWriter printWriter;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
try{
socket = new Socket("localhost",63400);
System.out.println("Enter Query to execute:");
sc.nextLine(); //Expecting the user to hit "enter" when done
printWriter = new PrintWriter(socket.getOutputStream(),true);
printWriter.println(sc);
} catch(Exception e) {
System.out.println(e);
}
}
}
假设我必须执行一个SELECT查询,我从应用程序的客户端在控制台中输入:
SELECT show_title FROM shows WHERE show_id=1
我得到的只是这个错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'java.util.Scanner[delimiters=\p+][position=46][match valid=true][need input=fals' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1557)
at Server.main(Server.java:54)
此错误与我输入查询的方式有关还是其他原因?您的客户端出错,它正在发送
扫描仪的输出:
printWriter.println(sc);
翻译为:
new PrintWriter(s.getOutputStream()).println(new Scanner(System.in).toString());
此外,让服务器执行任意SQL是一个坏主意,而不是通过发送几个参数来编码预定义的操作
例:
使用PreparedStatement
设置参数我读过关于PreparedStatement的文章,但我认为它在我的案例中没有多大用处。我必须能够执行不同的查询,而不仅仅是类似于我给出的示例。如果我正确理解了你的答案。
action=get_results
id_x=2