服务器端方法用sql添加动物名称,spcies id,动物描述到ms访问数据库
//declarations
private int port;
Socket soc; //client socket
ServerSocket lstn; //server socket
InputStream in;
DataInputStream inData;
OutputStream out;
DataOutputStream outData;
static String client, username, password, request;
boolean connected;
static String model;
static Connection conn;
static Statement st;
static ResultSet rec;
//login
static String[] ConnectOptionNames = {"Connect", "Cancel"};
static String ConnectTitle = "Set hostname";
//database and sockets
boolean isConnect;
boolean connected;
private void startServer() {
try {
lstn = new ServerSocket(port);
txtWin.append("Listening on port number: " + port + "\n");
soc = lstn.accept();
connected = true;
in = soc.getInputStream();
inData = new DataInputStream(in);
out = soc.getOutputStream();
outData = new DataOutputStream(out);
client = inData.readUTF();
etc...(the rest of this code is socket close and catch statements
txtWin.append("Connection established with " + client + "\n");
while (connected) {
model = "";
txtWin.append("Awaiting new request");
request = inData.readUTF();
txtWin.append("Request from client: \t" + request + "\n");
//add to Animal Table methode server side
public boolean addAnimal(String name, String desc, int ID) {
try {
boolean match = Pattern.matches("[a-z]+", name);
int count = 0;
boolean act = false;
if (match) /*No numbers are allowed in the animal name*/ {
connectDb();
java.sql.Statement stmt = conn.createStatement();
String sql1 = "SELECT * FROM Animals";
ResultSet rs1 = stmt.executeQuery(sql1);
while (rs1.next()) {
count = Integer.parseInt(rs1.getString(1));
}
//Check to see if the Species_ID exists
String sql2 = "SELECT * FROM Species WHERE species_id= " + ID;
ResultSet rs = stmt.executeQuery(sql2);
while (rs.next()) {
String sql3 = "INSERT INTO Animals (animal_id, animal_name, description, species_id) VALUES (" + ++count + ", '" + name + "', '" + desc + "', " + ID + ")";
int rs2 = stmt.executeUpdate(sql3);
return true;
}
return act;
} else {
return act;
}
} catch (SQLException e) {
System.out.println("Server, addAnimal: " + e);
connected = false;
return false;
}
}
***上面是我的应用程序服务器端这个函数是为了从客户端添加3个字符串到ms访问数据库。从名为delegate的switch case调用此方法。它工作正常!!!
接下来是客户端,我发现添加动物错误无效。***
//在服务器中添加动物的方法
//sockets and stream declarations
private InputStream in;
private DataInputStream Datain;
private OutputStream out;
private DataOutputStream Dataout;
private Socket soc;
static private String host, server;
int port;
public String search;
static Connection conn;
Socket clientSocket = null;
public boolean addAnimal(String name, String desc, int ID) {
boolean fromServer;
try {
startClient();
if (isConnect) {
Dataout.writeUTF("addAnimal");
Dataout.writeUTF(name);
Dataout.writeUTF(desc);
Dataout.writeInt(ID);
fromServer = Datain.readBoolean();
//closeConnection();
return fromServer;
} else {
System.out.println("ERROR ADD ANIMAL NOT FUNCTIONING");
}
//closeConnection();
return false;
} catch (IOException | NullPointerException e) {
System.out.println("ServerConnect, addSpecies: " + e);
//closeConnection();
return false;
}
}
//Method for add button in admin add delete page/form
private void addbtnActionPerformed(java.awt.event.ActionEvent evt) {
boolean addM = false;
if (tabelbtn.isSelected()) {
if (addnametxt.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please fill in all infomation into spaces provided.\n It is important to Remember fill in a discription");
} else {
addM = addSpecies(addnametxt.getText().toLowerCase());
}
} else {//if btn is false
if (addnametxt.getText().equals("") || adddiscriptiontxt.getText().equals("") || addspeciestxt.getText().equals(""))/*Checks to see if all the data is entered*/ {
JOptionPane.showMessageDialog(null, "Please fill in all details on the form.\n It is important to Remember fill in a discription");
} else {
**(1038)** addM = addAnimal(addnametxt.getText().toLowerCase(), adddiscriptiontxt.getText().toLowerCase(), Integer.parseInt(addspeciestxt.getText()));
}
}
我的控制台上的错误 启动客户 线程中的异常" AWT-EventQueue-0" java.lang.NumberFormatException:对于输入字符串:" canine" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at mgl5client.MGL5client.addbtnActionPerformed(MGL5client.java:1038) (上面标有1038行) at mgl5client.MGL5client.access $ 700(MGL5client.java:22) **(第22行)**公共类MGL5client扩展JFrame实现ActionListener
at mgl5client.MGL5client $ 10.actionPerformed(MGL5client.java:392) **(第392行)** addbtnActionPerformed(evt);