Created
December 9, 2012 06:21
-
-
Save anonymous/4243576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package botan; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.sql.Connection; | |
import java.sql.DatabaseMetaData; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
public class DBManager { | |
private String currentDBName = ""; | |
public DBManager(){ | |
setDBName("sampledb"); | |
} | |
public void setDBName(String dbName){ | |
currentDBName = dbName; | |
return; | |
} | |
private Connection connect(){ | |
Connection con = null; | |
try { | |
// ドライバクラスをロード | |
Class.forName("com.mysql.jdbc.Driver"); | |
// データベースへ接続 | |
con = DriverManager.getConnection("jdbc:mysql://localhost/" + currentDBName,"username","password"); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return con; | |
} | |
public void insertStatusIdsLong(Long statusId, String tableName){ | |
PreparedStatement ps = null; | |
Connection con = connect(); | |
try{ | |
String sql = "INSERT INTO `" + tableName + "` (status_id) VALUES (" | |
+ statusId + ");"; | |
ps = con.prepareStatement(sql); | |
ps.executeUpdate(); | |
}catch(SQLException e){ | |
// e.printStackTrace(); | |
}catch(OutOfMemoryError e){ | |
e.printStackTrace(); | |
} | |
finally{ | |
closeRsPsCon(null, ps, con); | |
} | |
} | |
public void closeRsPsCon(ResultSet rs, PreparedStatement ps, Connection con){ | |
try{ | |
if(rs != null) | |
rs.close(); | |
if(ps != null) | |
ps.close(); | |
if(con != null) | |
con.close(); | |
}catch (SQLException e){ | |
e.printStackTrace(); | |
} | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment