明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

詳細(xì)說明MySQL的容易使用與JDBC示例代碼分享

[摘要]MySql簡(jiǎn)單操作<code>//啟動(dòng)mysqlnet start mysql//登陸mysql -u root -p//創(chuàng)建建數(shù)據(jù)庫create database mydb;create database test;//刪除數(shù)據(jù)庫drop database test;//使用數(shù)據(jù)庫u...

MySql簡(jiǎn)單操作

<code>//啟動(dòng)mysql
net start mysql
 
//登陸
mysql -u root -p
 
 
//創(chuàng)建建數(shù)據(jù)庫
create database mydb;
create database test;
 
//刪除數(shù)據(jù)庫
drop database test;
 
//使用數(shù)據(jù)庫
use mydb;
 
//創(chuàng)建表
create table mytable(name varchar(20),sex char);
 
//顯示數(shù)據(jù)庫中所有表
show tables;
 
//增加一列
alter table mytable add age int;
 
//插入
insert mytable value("liu",'f',22);
insert mytable value("wang",'m',22);
insert mytable(name,sex) value("li",'f');
insert mytable value("zhao",'m',22);
 
//刪除表中項(xiàng)
delete from mytable where name=”liu“;
delete from mytable where name=”li“;
 
//更新
update mytable set age = 23 where name = "wang";
 
//查詢
select * from mytable;
 
</code>

JDBC

JDBC驅(qū)動(dòng)程序分為4類

JDBC-ODBC橋 部分本地API,部分Java驅(qū)動(dòng)程序 JDBC網(wǎng)絡(luò)純Java驅(qū)動(dòng)程序 本地協(xié)議Java驅(qū)動(dòng)程序

JDBC的示例

<code>import java.sql.*;
import com.mysql.jdbc.Driver;
public class JDBCMySql {
     
    public void operateMySql() throws SQLException, ClassNotFoundException{
         
        String driverName ="com.mysql.jdbc.Driver";
        String URL = "jdbc:mysql://127.0.0.1:3306/mydb";
        String sql = "SELECT * FROM mydb.mytable";
        String username = "root";
        String password = "";
        Connection conn = null;
        try{
         
        //加載驅(qū)動(dòng)
        Driver.class.forName(driverName);
         
        //建立連接
        conn = DriverManager.getConnection(URL,username,password);
         
        //創(chuàng)建statement來執(zhí)行sql語句
        Statement ps =  conn.createStatement();
         
        //結(jié)果集處理
        ResultSet rs = ps.executeQuery(sql);
        while(rs.next()) {        
        System.out.println(rs.getString("name")+","+rs.getString("sex")+","+rs.getInt("age"));    
            }
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 
        catch(SQLException e){
            e.printStackTrace();
        }
         
        //關(guān)閉連接
        finally{
            if(conn!=null){
                try{
                    conn.close();
                }catch(SQLException e){
                    e.printStackTrace();
                }
            }
        }   
    }   
}</code>

建立一個(gè)test case來驗(yàn)證一下

<code>
import static org.junit.Assert.*;
 
import java.sql.SQLException;
 
import org.junit.Before;
import org.junit.Test;
 
public class JDBCTest {
 
    @Test
    public void test() throws ClassNotFoundException, SQLException {
        JDBCMySql jm = new JDBCMySql();
        jm.operateMySql();
    }
 
}</code>

執(zhí)行結(jié)果

1252.png

1253.png

以上就是詳解MySQL的簡(jiǎn)單使用和JDBC示例代碼分享的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。