DB接続

JAVAからMySQLのDBにアクセスする方法です。

import java.sql.*;

Connection conn = null;
CallableStatement cs = null;
String url = "jdbc:mysql://localhost/dbName?characterEncoding=UTF-8";
String user = "dbUser";
String pass = "dbPass";
try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = DriverManager.getConnection(url, user, pass);
 Statement stmt = null;

 String sql="insert into table (col1,col2,col3) ";
 sql += " values('"+ins1+"','"+ins2+"','"+ins3+"')";

 stmt = conn.createStatement();
 int kekka = stmt.executeUpdate(sql);
}catch(ClassNotFoundException e){
 //class ng
}catch(SQLException e){
 //sql ng
}catch(Exception e){
 //exception
}finally{
 try{
  if(conn != null){
   conn.close();
  }
 }catch(SQLException e){
  //sql close err
 }
}

jdbc:mysql://localhost/dbName?characterEncoding=UTF-8
javaからmysqlサーバー(localhost)のdbNameデータベースにUTF-8で接続するという感じです。
●int kekka = stmt.executeUpdate(sql);
insert文ですのでinsertした数が返ってきます。