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语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于计算机知识竞答的问题,电脑知识竞赛题
- 下一篇: 文件上传java前端怎么写_做一个文件上