- 有两个表
chat_users
和chat_lines
。 - 插入和选择适用于
chat_user
- 更新不起作用
chat_user
_ - 插入也
chat_lines
不起作用
chat_user_model.java的源代码列表
package model;
import Beans.ChatLineBeans;
import Beans.ChatUserBeans;
import java.security.Timestamp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author user
*/
public class chat_user_model extends DBConnection {
public static void add_User(ChatUserBeans user ) throws ClassNotFoundException {
try {
Connection conn = createConnection();
PreparedStatement s;
s = conn.prepareStatement (
"INSERT INTO chat_users (name, gravatar) VALUES(?,?)");
s.setString (1,user.getuserName());
s.setString (2, user.getgravatar());
int count = s.executeUpdate ();
s.close ();
conn.close();
//System.out.println (count + " rows were inserted");
} catch (SQLException ex) {
Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static ChatUserBeans find_user(String name ) {
ChatUserBeans user=new ChatUserBeans();
try {
Connection con = createConnection();
PreparedStatement stmt = con.prepareStatement("select * from chat_users where name=? ");
stmt.setString(1, name);
ResultSet result= stmt.executeQuery();
while(result.next())
{
user.setuserName(result.getString("name"));
user.setgravatar(result.getString("gravatar"));
}
stmt.close();
stmt.close();
con.close();
} catch (SQLException ex) {
Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
}
return user ;
}
public static void add_line(ChatLineBeans line ) throws ClassNotFoundException {
try {
Connection conn = createConnection();
PreparedStatement s;
s = conn.prepareStatement (
"INSERT INTO chat_lins (aurther, gravatar,text) VALUES(?,?,?)");
s.setString (1,line.getauther());
s.setString (2, line.getgravatar());
s.setString (3, line.gettext());
int count = s.executeUpdate ();
s.close ();
conn.close();
//System.out.println (count + " rows were inserted");
} catch (SQLException ex) {
Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void delete_User(String name ) throws ClassNotFoundException {
try {
Connection conn = createConnection();
PreparedStatement s;
s = conn.prepareStatement (
"DLETE FROM chat_users WHERE name=?");
s.setString (1,name);
int count = s.executeUpdate ();
s.close ();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(chat_user_model.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
连接文件是
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBConnection {
private static String hostName = "localhost";
private static String port = "3306";
private static String dbName ="120623085702";
private static String userName = "root";
private static String password="";
public static Connection createConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + hostName + ":" + port + "/" + dbName;
System.out.println(url);
Connection con = DriverManager.getConnection(url,userName,password);
return con;
} catch (SQLException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
有关如何解决此问题的任何建议。
提前致谢。