guys i have tryed to make a database with roles i did it this way but dunno why sometimes it bugs and doesnt update as it should :
Code:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*"%>
<%@ page import="com.goldhuman.auth.AuthFilter"%>
<%@ page import="com.goldhuman.service.interfaces.LogInfo"%>
<%@ page import="com.goldhuman.service.interfaces.SimpleRoleBean"%>
<%@ page import="com.goldhuman.service.interfaces.GMService"%>
<%@ page import="com.goldhuman.service.GMServiceImpl"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<%@ page import="com.goldhuman.util.LocaleUtil"%>
<%@ page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>
<%@page import="protocol.*"%>
<%@page import="java.text.*"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.goldhuman.Common.Octets"%>
<%@page import="com.goldhuman.IO.Protocol.Rpc.Data.DataVector"%>
<%@page import="org.apache.commons.lang.StringEscapeUtils"%>
<%@ page import="com.goldhuman.auth.AuthFilter"%>
<%@ page import="com.goldhuman.service.interfaces.LogInfo"%>
<%@ page import="com.goldhuman.service.interfaces.CashInfoBean"%>
<%@ page import="com.goldhuman.service.interfaces.GMService"%>
<%@ page import="com.goldhuman.service.GMServiceImpl"%>
<%@page import="org.apache.commons.logging.LogFactory"%>
<%@page import="com.goldhuman.util.LocaleUtil"%>
<html>
<head>
<link href="../include/style.css" rel="stylesheet" type="text/css">
<title></title>
</head>
<body>
<center>
<br>
<%
int roleid=0;
RoleBean role=null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement st =null;
try {
/* Create string of connection url within specified format with machine name,
port number and database name. Here machine name id localhost and
database name is usermaster. */
String connectionURL = "jdbc:mysql://localhost:3306/dbo";
// declare a connection by using Connection interface
// Load JBBC driver "com.mysql.jdbc.Driver"
Class.forName("com.mysql.jdbc.Driver").newInstance();
/* Create a connection by using getConnection() method that takes parameters of
string type connection url, user name and password to connect to database. */
con = DriverManager.getConnection(connectionURL, "root","root");
LogInfo info = null;
st = con.createStatement() ;
st.executeUpdate("DROP TABLE IF EXISTS roles2");
st.executeUpdate
(
"CREATE TABLE roles2(" +
"userid int(11) NOT NULL, "+
"Roleid int(11) NOT NULL, " +
"name varchar(32) NOT NULL, " +
"class int(11) NOT NULL, " +
"gender int(11) NOT NULL, " +
"level int(11) NOT NULL, " +
"reputation int(11) NOT NULL, " +
"cultivation int(11) NOT NULL, " +
"redname int(11) NOT NULL) "+
"ENGINE=MYISAM DEFAULT CHARSET=utf8;"
);
String queryString="SELECT MAX(id) FROM users";
rs = st.executeQuery(queryString);
int maxUserId=0;
while ( rs.next() ) maxUserId = rs.getInt(1);
int uid=32;
while(uid<=maxUserId){
GMService gs = new GMServiceImpl();
info = new LogInfo(uid, "", LocaleUtil.getMessage(request,"role_rolelist_showrolelist"));
Vector v = gs.getRolelist(uid,info);
if (v != null) {
for (int i = 0; i < v.size(); i++) {
SimpleRoleBean bean = (SimpleRoleBean) v.get(i);
roleid=bean.roleid;
role = GameDB.get( roleid );
session.setAttribute( "gamedb_rolebean", role );
int updateQuery=0;
int level=role.status.level;
String name =role.base.name.getString();
String Class=""+role.base.cls;
int sex=(int) role.base.gender;
int rep=role.status.reputation;
int culti=role.status.level2;
int redHours=role.status.invader_time;
ps = con.prepareStatement("INSERT INTO roles2 VALUES (?,?,?,?,?,?,?,?,?)");
ps.setInt(1,uid);
ps.setInt(2,roleid);
ps.setString(3, name);
ps.setString(4,Class);
ps.setInt(5, sex);
ps.setInt(6, level);
ps.setInt(7, rep);
ps.setInt(8, culti);
ps.setInt(9, redHours);
updateQuery = ps.executeUpdate();
if (updateQuery != 0) {
}
}
}
uid=uid+16;
// check weather connection is established or not by isClosed() method
}
}
catch(Exception ex){
out.println("Unable to connect to database."+ex);
}
boolean success = false;
try {
success = GameDB.update( role );
if( success && role.base.id >= 16 && role.base.id < 31 )
{
int newroleid = ( 0 == role.base.id % 2 ) ? (role.base.id+1) : (role.base.id-1);
role.base.id = newroleid;
success = GameDB.update( role );
}
}
catch (Exception e) { out.println(e.toString()); return; }
%>
<TABLE style="background-color: #E3E4FA;"
WIDTH="100%" border="5" >
<tr><th>Data is inserted successfully
in database.</th></tr>
</table>
</center>
</body>
</html>