JDBC的基本操作
JDBC的基本操作:
鏈接數(shù)據(jù)庫并創(chuàng)建一個學生表,對數(shù)據(jù)庫的詳細操作可以點擊文章底部的文章”MySQL數(shù)據(jù)庫的常用操作“鏈接。
實現(xiàn)代碼如下:
package star.july; /*** JDBC的基本操作* 鏈接數(shù)據(jù)庫并創(chuàng)建一個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、注冊驅(qū)動程序Class.forName("com.mysql.jdbc.Driver");//2、鏈接數(shù)據(jù)庫,獲得連接對象String url = "jdbc:mysql://localhost:3306/day16";String root = "root";String password = "root";Connection conn = DriverManager.getConnection(url,root,password); //3、創(chuàng)建Statement對象,用于發(fā)送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對象發(fā)送Sql語句,返回的是影響的行數(shù)int count = stmt.executeUpdate(sql);//6、查看結(jié)果,創(chuàng)建表通常影響的結(jié)果為0System.out.println("影響的"+count+"行數(shù)");//7、關(guān)閉資源stmt.close();conn.close();} }
總結(jié)
- 上一篇: Java中使用HashMap,TreeS
- 下一篇: JDBC进阶:调用数据库,将文件或其他字