欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 运维知识 > 数据库 >内容正文

数据库

java连接mysql执行ddl_dljd_(007_009)_jdbc执行DQL/DML/DDL语句

发布时间:2024/7/23 数据库 60 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java连接mysql执行ddl_dljd_(007_009)_jdbc执行DQL/DML/DDL语句 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

packageedu.aeon.jdbc;importjava.sql.Connection;importjava.sql.Driver;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/*** [说明]:测试jdbc

*@authoraeon(qq:1584875179)

**/

public classTest {/*** jdbc执行DQL语句*/

public static voidtestDQL(){

Connection connection=null;

Statement statement=null;

ResultSet resultSet=null;try{

Driver driver=newcom.mysql.jdbc.Driver();

DriverManager.registerDriver(driver);

String url="jdbc:mysql://localhost:3306/jdbc";

String username="root";

String password="root";

connection=DriverManager.getConnection(url, username, password);

statement=connection.createStatement();

String dql_sql="select * from test;";

resultSet=statement.executeQuery(dql_sql);

System.out.println("序号\t名 称");while(resultSet.next()){int id=resultSet.getInt("id");

String name=resultSet.getString("name");

System.out.println(id+"\t"+name);

}

}catch(SQLException e) {

e.printStackTrace();

}finally{try{if(null!=resultSet){

resultSet.close();

}

}catch(SQLException e) {

e.printStackTrace();

}try{if(null!=statement){

statement.close();

}

}catch(SQLException e) {

e.printStackTrace();

}try{if(null!=connection){

connection.close();

}

}catch(SQLException e) {

e.printStackTrace();

}

}

}/*** jdbc执行DML语句*/

public static voidtestDML(){

Connection connection=null;

Statement statement=null;try{

Driver driver=newcom.mysql.jdbc.Driver();

DriverManager.registerDriver(driver);

String url="jdbc:mysql://localhost:3306/jdbc";

String username="root";

String password="root";

connection=DriverManager.getConnection(url, username, password);

statement=connection.createStatement();

String dml_sql="insert into test(name) values('name6')";int num=statement.executeUpdate(dml_sql);

System.out.println("受影响的行数为:"+num);

}catch(SQLException e) {

e.printStackTrace();

}finally{try{if(null!=statement){

statement.close();

}

}catch(SQLException e) {

e.printStackTrace();

}try{if(null!=connection){

connection.close();

}

}catch(SQLException e) {

e.printStackTrace();

}

}

}/*** jdbc执行DDL语句*/

public static voidtestDDL(){

Connection connection=null;

Statement statement=null;try{

Driver driver=newcom.mysql.jdbc.Driver();

DriverManager.registerDriver(driver);

String url="jdbc:mysql://localhost:3306/jdbc";

String username="root";

String password="root";

connection=DriverManager.getConnection(url, username, password);

statement=connection.createStatement();

String ddl_sql="create table users(id int(4) primary key auto_increment,name varchar(10),password varchar(16));";int num=statement.executeUpdate(ddl_sql);

System.out.println("受影响的行数为:"+num);

}catch(SQLException e) {

e.printStackTrace();

}finally{try{if(null!=statement){

statement.close();

}

}catch(SQLException e) {

e.printStackTrace();

}try{if(null!=connection){

connection.close();

}

}catch(SQLException e) {

e.printStackTrace();

}

}

}/*** jdbc使用excute方法同时可以执行DQL/DML/DDL语句*/

public static voidtestDQLDMLDDL(){

Connection connection=null;

Statement statement=null;

ResultSet resultSet=null;try{

Driver driver=newcom.mysql.jdbc.Driver();

DriverManager.registerDriver(driver);

String url="jdbc:mysql://localhost:3306/jdbc";

String username="root";

String password="root";

connection=DriverManager.getConnection(url, username, password);

statement=connection.createStatement();

String sql="insert into test(name) values('name6')";if(statement.execute(sql)){//如果返回true则表示执行的是DQL,有结果集返回,如果为false则表示返回的是更新计数器

resultSet=statement.getResultSet();

System.out.println("序号\t名 称");while(resultSet.next()){int id=resultSet.getInt("id");

String name=resultSet.getString("name");

System.out.println(id+"\t"+name);

}

}else{//如果为false则表示返回的是更新计数器

int num =statement.getUpdateCount();

System.out.println("更新了"+num+"条数据!");

}

}catch(SQLException e) {

e.printStackTrace();

}finally{try{if(null!=statement){

statement.close();

}

}catch(SQLException e) {

e.printStackTrace();

}try{if(null!=connection){

connection.close();

}

}catch(SQLException e) {

e.printStackTrace();

}

}

}public static voidmain(String[] args) {//testDQL();//testDML();//testDDL();

testDQLDMLDDL();

}

}

总结

以上是生活随笔为你收集整理的java连接mysql执行ddl_dljd_(007_009)_jdbc执行DQL/DML/DDL语句的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。