我有一个连接到bigRock上托管的MySQL服务器的java系统应用程序,当我尝试从数据库中检索数据时应用程序工作正常,但在插入时它显示以下错误。
SEVERE: null
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Connection reset by peer: socket write error
STACKTRACE:
java.net.SocketException: Connection reset by peer: socket write error
我的连接类看起来像
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sun.jdbc.odbc.JdbcOdbc;
public class DataBaseFunctionting
{
Connection conn =null;
PreparedStatement preparedStatement=null;
boolean reachable ;
private void connectionCheck() // checks wether client is connected to the internet and host is reachable
{
Socket socket = null;
reachable = false;
try
{
socket = new Socket("www.rinchedmurtipujaksang.com", 80);
reachable = true;
}
catch (UnknownHostException ex)
{
Logger.getLogger(addNewArea.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(addNewArea.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
if (socket != null) try { socket.close(); } catch(IOException e) {}
}
}
public Connection connect()
{
connectionCheck();
if(!reachable)
{
JOptionPane.showMessageDialog(null,"Unable to reaach the Server.\nCheck your internet connection OR contact host.");
return null;
}
try
{
String dbURL = "jdbc:mysql://rinchedmurtipujaksang.com:3306/rin*******a";
String username ="rinc*******min";
String password = "****";
conn = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(dbURL, username, password);
return conn;
}
catch (Exception e)
{
Logger.getLogger(DataBaseFunctionting.class.getName()).log(Level.SEVERE, null, e);
System.out.println("Error : "+e);
return null;
}
}
public void disconnect() throws SQLException
{
conn.close();
}
}
和从前端插入数据到数据库的函数看起来像
public void addMemberToDB()
{
try
{
DB=new DataBaseFunctionting();
con=DB.connect();
PreparedStatement preparedstatement=con.prepareStatement("insert into Member values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
preparedstatement.setObject(1,memberID);
preparedstatement.setObject(2,familyIDBox.getText());
preparedstatement.setObject(3,FName.getText());
preparedstatement.setObject(4,MName.getText());
preparedstatement.setObject(5,LName.getText());
preparedstatement.setObject(6,FName.getText()+" "+MName.getText()+" "+LName.getText());
if(male.isSelected())
{
preparedstatement.setObject(7,"Male");
}
else preparedstatement.setObject(7,"Female");
preparedstatement.setObject(9,new DateTime(DOB.getDate()).toString("yyyy-MM-dd"));
preparedstatement.setObject(8,livingStatus.getSelectedItem());
if(livingStatus.getSelectedIndex()==1)
{
DateTime dateTime=new DateTime(DOD.getDate());
preparedstatement.setObject(10,dateTime.toString("yyyy-MM-dd"));
}
else preparedstatement.setObject(10,null);
preparedstatement.setObject(11,workProfile.getText());
if(RelationshipStatus.getSelectedItem().toString().equals("Single"))
{
preparedstatement.setObject(12,0);
}
else preparedstatement.setObject(12,couple_id.getText()); // fix couple table if married.
if(noNo.isSelected())
{
preparedstatement.setObject(13,null);
}
else preparedstatement.setObject(13,pNo.getText());
if(noImage.isSelected())
{
preparedstatement.setBytes(14,null);
}
else
{
int s=0;
byte[] imageData;
File image;
image = new File(photoUrl.getText());
FileInputStream fis=new FileInputStream(image);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] buff=new byte[1024];
for(int readNum;(readNum=fis.read(buff))!=-1;)
{
bos.write(buff,0,readNum);
}
imageData=bos.toByteArray();
preparedstatement.setBytes(14,imageData);
}
preparedstatement.setObject(15,fbUrl.getText());
System.out.println(preparedstatement);
preparedstatement.executeUpdate();
DB.disconnect();
}
catch (SQLException | FileNotFoundException ex)
{
Logger.getLogger(newMember.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(newMember.class.getName()).log(Level.SEVERE, null, ex);
}
}
任何人都可以帮我摆脱这个错误吗?