【Java】操作mysql数据库

package bd;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JDBCTest {
    public static void main(String[] args) {
 
        // 驱动程序名
        String driver = "com.mysql.jdbc.Driver";
        // URL指向要访问的数据库名
        String url = "jdbc:mysql://192.168.238.10:3306/F4base";
        // MySQL配置时的用户名
        String user = "";
        // MySQL配置时的密码
        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用来执行SQL语句
            Statement statement = conn.createStatement();
 
            for(int i=0;i<10;i++)
            {
                String insertSql = "insert into Test(cmd,name,time) values("+i+","+(2*i)+",20170402)";
        //      statement.executeUpdate(insertSql);
            }
            // 要执行的SQL语句
            String sql = "select * from Test";
            // 结果集
            ResultSet rs = statement.executeQuery(sql);
        //  String name = null;
 
            while (rs.next())
            {
                System.out.println(rs.getString("cmd") + "  " + rs.getString("name") + "  "+ rs.getString("time"));
            }
             
            rs.close();
            conn.close();
 
        } catch (ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
原文地址:https://www.cnblogs.com/tiandsp/p/7467867.html