Skip to content

Instantly share code, notes, and snippets.

@namiken
Created June 17, 2014 09:06
Show Gist options
  • Save namiken/d8d78cc0958bedbac80e to your computer and use it in GitHub Desktop.
Save namiken/d8d78cc0958bedbac80e to your computer and use it in GitHub Desktop.
package 自習4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MysqlConnector {
Connection con;
Statement stmt;
public MysqlConnector() {
try {
// JDBCドライバの登録
String driver = "org.gjt.mm.mysql.Driver";
// データベースの指定
String server = "localhost"; // MySQLサーバ ( IP または ホスト名 )
String dbname = "sampledb"; // データベース名
String url = "jdbc:mysql://" + server + "/" + dbname + "?useUnicode=true&characterEncoding=EUC_JP";
String user = "root"; // データベース作成ユーザ名
String password = "root"; // データベース作成ユーザパスワード
Class.forName (driver);
// データベースとの接続
con = DriverManager.getConnection(url, user, password);
stmt = con.createStatement ();
} catch (SQLException e) {
System.err.println("SQL failed.");
e.printStackTrace ();
} catch (ClassNotFoundException ex) {
ex.printStackTrace ();
}
}
public Statement getStatement() {
return stmt;
}
public void close() throws SQLException {
stmt.close();
con.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment