「java入門」com.mysql.jdbc.Driverでmysqlに接続するサンプルコード
javaコード:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlDemo {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Class.forName(“com.mysql.jdbc.Driver");
//org.gjt.mm.mysql.Driver
Connection conn = null;
Statement stmt = null;
ResultSet rs =null;
String url = “jdbc:mysql://172.17.2.200:3306/mysql";
conn = DriverManager.getConnection(url,"root","123456″);
stmt = conn.createStatement();
rs = stmt.executeQuery(“select * from user");
while(rs.next())
{
System.out.println(rs.getObject(1));
}
stmt.close();
conn.close();
}
}