第一次学jdbc
快速入门程序
package com.JDBC.TEST;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws SQLException {
//注册驱动 new com.mysql.cj.jdbc.Driver().var; 然后按enter或者new Driver();按ctrl+alt+v
Driver driver = new Driver();
//连接数据库
String url = "jdbc:mysql://localhost:3306/jdbc_study";
Properties prop = new Properties();
prop.setProperty("user","root");
prop.setProperty("password","root");
Connection connect = driver.connect(url,prop );//网络连接
//执行sql
String sql = "insert into s value('2010530223','欧健','男','20','CS')";
Statement statement = connect.createStatement(); //生成的这个statement 是帮我们执行这个SQL语句的
int rows = statement.executeUpdate(sql);//如果是dml语句,放回的是影响的行数
System.out.println(rows >0?"成":"失败");
//关闭连接
statement.close();
connect.close();
}
}
记得在这写程序之前一定要有拷贝mysql.jar的动作,而且MySQL下也要有相应的表哦



