JAVA MySQL 插入 查询
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
//import java.util.*;
public class maino {
public static void main(String[] args) {
String driver ="com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/fei";
String user ="root";
String password ="";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql ="select * from info";
String sql1 = "INSERT INTO info VALUES ('Bziill', '93rd', '17')";
statement.executeUpdate(sql1);
ResultSet rs = statement.executeQuery(sql);
System.out.println("----------------");
System.out.println("Result is :");
String name=null;
while (rs.next()) {
System.out.println(rs.getString("Name")+": "+rs.getString("Age"));
}
} catch(ClassNotFoundException e) {
System.out.println("Sorry,cant find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}