The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
untitled Java 5 Code
Posted by: db | August 26, 2009 @ 4:37am
Java 5 Code
[
Download
]
public class Database { private static Database instance = new Database(); private static Connection conn = null; // folgende Zeile nur bei Version Hashtable Hashtable<String, PreparedStatement> prepStatements = new Hashtable<String, PreparedStatement>(); // folgende Zeile nur bei Version globale Variablen PreparedStatement banUserPrep; public static Database getInstance() { return Database.instance; } private DatabaseConnection() { String connURL = ""; String username = ""; String pass = ""; try { conn = DriverManager.getConnection(connURL, username, pass); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } // alle preparedstatements zur db senden und ebenso in der hashtabel speichern um darauf zugreifen zu k�nnen try { // folgende Zeile nur bei Version Hashtable prepStatements.put("banUserPrep",conn.prepareStatement("INSERT INTO ...")); // folgende Zeile nur bei Version globale Variablen banUserPrep = conn.prepareStatement("INSERT INTO ..."); } catch (SQLException e) {} } public boolean banUser(int userid, int duration, String desc) { // folgende Zeile nur bei Version Hashtable PreparedStatement tempState = prepStatements.get("banUserPrep"); // folgende Zeile nur bei Version globale Variablen PreparedStatement tempState = banUserPrep; // get actual time in milliseconds long banstart = java.util.Calendar.getInstance().getTimeInMillis(); long banend = banstart + duration; int changedRows = 0; try { tempState.setInt(1, userid); tempState.setLong(2, banstart); tempState.setLong(3, banend); tempState.setString(4, desc); changedRows = tempState.executeUpdate(); } catch (SQLException e) {} if(changedRows == 1){ // es wurde eine zeile geaendert -> erfolg beim schreiben return true; } else{ // keine zeile geaendert return false; } } }
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.40 / 8
99 Votes