如何使用 rmi 将记录插入 mysql(jdbc) in java?
how to insert a record into mysql(jdbc) in java using rmi?
我正在 Java 使用 swings 制作一个项目,JDBC 我想使用 RMI 将记录插入我的 MySQL 数据库。在我使用 RMI 之前一切正常。
当我使用 RMI 时,一切正常,但我的数据库 table 没有反映正在创建的新条目。
如何做到这一点?
这是我的远程实现代码
package server;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class myremote_imp extends UnicastRemoteObject implements myremote{
String URL="jdbc:mysql://localhost:3306/project";
String user="root";
String pass="root";
public myremote_imp()throws RemoteException
{}
public void putdata(String s6,String s7,String s8,String s1,String s2,String s3,String s4,String s5,String User,String Pass,String P_name,String P_price)
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection C=DriverManager.getConnection(URL,user,pass);
System.out.println("connection Established");
PreparedStatement S=C.prepareStatement("insert into customers values(?,?,?,?,?,?,?,?,?,?,?,?)");
S.setString(1,s6);
S.setString(2,s7);
S.setString(3,s8);
S.setString(4,s1);
S.setString(5,s2);
S.setString(6,s3);
S.setString(7,s4);
S.setString(8,s5);
S.setString(9,User);
S.setString(10,Pass);
S.setString(11,P_name);
S.setString(12,P_price);
//System.out.println(s6+s7+s8+s1+s2+s3+s4);just to check if values are being passed by the remote correctly which is true
}
catch(Exception E)
{
System.out.println(E);
}
}
public static void main(String[] Args) throws RemoteException
{
try
{
Registry Rg = LocateRegistry.createRegistry(6199);
myremote mr = new myremote_imp();
Rg.rebind("remotedata",mr);
}
catch(Exception E)
{
System.out.println(E);
}
}
}
输出:
connection is establised
但更改未反映在数据库中。
but changes are not reflected in the database.
您忘记在 putdata()
方法的末尾调用 executeUpdate()。
.......................
S.setString(12,P_price);
S.executeUpdate();
我正在 Java 使用 swings 制作一个项目,JDBC 我想使用 RMI 将记录插入我的 MySQL 数据库。在我使用 RMI 之前一切正常。
当我使用 RMI 时,一切正常,但我的数据库 table 没有反映正在创建的新条目。
如何做到这一点?
这是我的远程实现代码
package server;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.*;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class myremote_imp extends UnicastRemoteObject implements myremote{
String URL="jdbc:mysql://localhost:3306/project";
String user="root";
String pass="root";
public myremote_imp()throws RemoteException
{}
public void putdata(String s6,String s7,String s8,String s1,String s2,String s3,String s4,String s5,String User,String Pass,String P_name,String P_price)
{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection C=DriverManager.getConnection(URL,user,pass);
System.out.println("connection Established");
PreparedStatement S=C.prepareStatement("insert into customers values(?,?,?,?,?,?,?,?,?,?,?,?)");
S.setString(1,s6);
S.setString(2,s7);
S.setString(3,s8);
S.setString(4,s1);
S.setString(5,s2);
S.setString(6,s3);
S.setString(7,s4);
S.setString(8,s5);
S.setString(9,User);
S.setString(10,Pass);
S.setString(11,P_name);
S.setString(12,P_price);
//System.out.println(s6+s7+s8+s1+s2+s3+s4);just to check if values are being passed by the remote correctly which is true
}
catch(Exception E)
{
System.out.println(E);
}
}
public static void main(String[] Args) throws RemoteException
{
try
{
Registry Rg = LocateRegistry.createRegistry(6199);
myremote mr = new myremote_imp();
Rg.rebind("remotedata",mr);
}
catch(Exception E)
{
System.out.println(E);
}
}
}
输出:
connection is establised
但更改未反映在数据库中。
but changes are not reflected in the database.
您忘记在 putdata()
方法的末尾调用 executeUpdate()。
.......................
S.setString(12,P_price);
S.executeUpdate();