package com.cn.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static Connection getConnection() throws SQLException,
java.lang.ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://192.168.100.59:3306/mydb";
String username = "remote";
String password = "config";
Connection con = DriverManager.getConnection(url, username, password);
return con;
}
public static void main(String args[]) {
try {
Connection con = getConnection();
Statement sql_statement = con.createStatement();
sql_statement.executeUpdate("delete from mytable");
sql_statement.executeUpdate("optimize table mytable");
for (int i = 0; i < 1000; i++) {
sql_statement.executeUpdate("insert into mytable() values ("
+ i + ")");
}
String query = "select * from mytable";
ResultSet result = sql_statement.executeQuery(query);
while (result.next()) {
int number = result.getInt("i");
System.out.println(number);
}
sql_statement.close();
con.close();
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException");
System.err.println(e.getMessage());
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
0 件のコメント:
コメントを投稿