3 # BorkNet Services Core
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 import java
.security
.*;
30 public static void main(String args
[])
32 CreateAccount ca
= new CreateAccount();
35 public CreateAccount()
38 Connection con
= openSqlConnection();
39 createUserAccount(con
);
40 closeSqlConnection(con
);
41 System
.out
.println("Useraccount added!");
45 private void testDriver()
49 Class
.forName ( "org.gjt.mm.mysql.Driver" );
51 catch ( java
.lang
.ClassNotFoundException e
)
53 System
.out
.println("MySQL JDBC Driver not found!");
58 private Connection
openSqlConnection()
62 String server
= readFromConsole("SQL Server host:");
63 String username
= readFromConsole("SQL username:");
64 String password
= readFromConsole("password:");
65 String database
= readFromConsole("Database:");
66 String url
= "jdbc:mysql://"+server
+"/"+database
+"?user="+username
+"&password="+password
;
67 Connection con
= DriverManager
.getConnection(url
);
72 System
.out
.println("Could not open SQL connection!");
78 private void createUserAccount(Connection con
)
82 PreparedStatement pstmt
;
83 pstmt
= con
.prepareStatement("INSERT INTO auths VALUES (?,?,?,?,?,?,?,?,?)");
84 pstmt
.setString(1,readFromConsole("Bot Account Username:"));
85 pstmt
.setString(2,encrypt(readFromConsole("Bot Account Password:")));
86 pstmt
.setString(3,readFromConsole("Bot Account E-Mail:"));
88 pstmt
.setBoolean(5,false);
90 pstmt
.setString(7,"0");
91 pstmt
.setString(8,"0");
92 pstmt
.setString(9,"0");
93 pstmt
.executeUpdate();
95 catch ( SQLException e
)
97 System
.out
.println ( "Error executing sql statement" );
98 StackTraceElement
[] te
= e
.getStackTrace();
99 System
.out
.println(e
.toString());
100 for(StackTraceElement el
: te
)
102 System
.out
.println("\tat "+el
.getClassName()+"."+el
.getMethodName()+"("+el
.getFileName()+":"+el
.getLineNumber()+")");
108 private String
readFromConsole(String question
)
110 System
.out
.println(question
);
111 Scanner in
= new Scanner(System
.in
);
112 return in
.nextLine();
115 //con = getConnection ( server, user, password, db);
116 private void closeSqlConnection(Connection con
)
124 System
.out
.println("MySQL connection failed to close.");
129 public String
encrypt(String plaintext
)
131 byte[] defaultBytes
= plaintext
.getBytes();
134 MessageDigest algorithm
= MessageDigest
.getInstance("MD5");
136 algorithm
.update(defaultBytes
);
137 byte messageDigest
[] = algorithm
.digest();
138 StringBuffer hexString
= new StringBuffer();
139 for (int i
=0;i
<messageDigest
.length
;i
++)
141 String hex
= Integer
.toHexString(0xFF & messageDigest
[i
]);
142 if(hex
.length()==1) hexString
.append('0');
143 hexString
.append(hex
);
145 return hexString
.toString();
147 catch(NoSuchAlgorithmException e
)
149 System
.out
.println ( "Error encrypting password." );