[Java] mysql 데이터베이스 조작

4530 단어
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

좋은 웹페이지 즐겨찾기