當前位置:
首頁 >
JDBC的基本操作
發布時間:2025/3/20
39
豆豆
JDBC的基本操作:
鏈接數據庫并創建一個學生表,對數據庫的詳細操作可以點擊文章底部的文章”MySQL數據庫的常用操作“鏈接。
實現代碼如下:
package star.july; /*** JDBC的基本操作* 鏈接數據庫并創建一個student表* */ import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement;import org.junit.Test;public class PractiseMySQL {@Testpublic static void main(String[] args) throws Exception {//1、注冊驅動程序Class.forName("com.mysql.jdbc.Driver");//2、鏈接數據庫,獲得連接對象String url = "jdbc:mysql://localhost:3306/day16";String root = "root";String password = "root";Connection conn = DriverManager.getConnection(url,root,password); //3、創建Statement對象,用于發送Sql語句Statement stmt = conn.createStatement(); //4、輸入要操作的sql語句String sql = "create table student (id int primary key auto_increment,name varchar(10),gender char(1));";//5、使用Statement對象發送Sql語句,返回的是影響的行數int count = stmt.executeUpdate(sql);//6、查看結果,創建表通常影響的結果為0System.out.println("影響的"+count+"行數");//7、關閉資源stmt.close();conn.close();} }
總結
- 上一篇: Java中使用HashMap,TreeS
- 下一篇: 自定义连接池