提交 9fb8193e 编写于 作者: CSDN-Ada助手's avatar CSDN-Ada助手

add questions

上级 d7c3af42
......@@ -7,7 +7,7 @@
## 答案
```
JDBC只可以用来建立数据库的连接
```
## 选项
......
{
"type": "code_options",
"author": "clong",
"source": "DriverType.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# JDBC Driver Type
以下不属于JDBC驱动类型的是:
## 答案
```
JDBC-TCP
```
## 选项
### A
```
JDBC-ODBC桥驱动
```
### B
```
JDBC-Native API
```
### C
```
JDBC-Net
```
{
"type": "code_options",
"author": "clong",
"source": "CRUD.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# JDBC CRUD
以下对数据库的操作错误的是:
## 答案
```java
import java.sql.*;
public class CRUD {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
String sql = "drop FROM student where id = 1";
stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
## 选项
### A
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CRUD {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
String sql = "INSERT INTO student " +
"VALUES (1, '小明', '男', 18)";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
### B
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CRUD {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
String sql = "UPDATE student " +
"SET age = 15 WHERE id = 1";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
### C
```java
import java.sql.*;
public class CRUD {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
String sql = "SELECT id, name, sex, age FROM student where id = 1";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
int age = rs.getInt("age");
String name = rs.getString("name");
String sex = rs.getString("sex");
System.out.println("id:" + id + ",name:" + name + ",sex" + sex + ",age:" + age);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
{
"type": "code_options",
"author": "clong",
"source": "PSQLConnection.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# PSQL Connection
以下连接postgre数据正确的是:
## 答案
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class PSQLConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "root", "root");
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE student " +
"(id INTEGER not NULL, " +
" name VARCHAR(255), " +
" sex VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
## 选项
### A
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class PSQLConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test");
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE student " +
"(id INTEGER not NULL, " +
" name VARCHAR(255), " +
" sex VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
### B
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class PSQLConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://root:root@localhost:5432/test");
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE student " +
"(id INTEGER not NULL, " +
" name VARCHAR(255), " +
" sex VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
### C
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class PSQLConnection {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://localhost/test", "root", "root");
Statement stmt = conn.createStatement();
String sql = "CREATE TABLE student " +
"(id INTEGER not NULL, " +
" name VARCHAR(255), " +
" sex VARCHAR(255), " +
" age INTEGER, " +
" PRIMARY KEY ( id ))";
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
{
"type": "code_options",
"author": "clong",
"source": "Transaction.md",
"exercise_id": "",
"notebook_enable": true
}
\ No newline at end of file
# Transaction
以下关于事务的写法能达到出错回退的目的的是:
## 答案
```java
import java.sql.*;
public class Transaction {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
try {
conn.setAutoCommit(false);
String insertSQL = "INSERT INTO student " +
"VALUES (1, '小明', '男', 18)";
stmt.executeUpdate(insertSQL);
String updateSQL = "UPDATE student " +
"SET age = 15 WHERE id = 1";
stmt.executeUpdate(updateSQL);
String deleteSQL = "delete FROM student where id = 1";
stmt.executeUpdate(deleteSQL);
conn.commit();
} catch (SQLException e) {
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
## 选项
### A
```java
import java.sql.*;
public class Transaction {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
try {
String insertSQL = "INSERT INTO student " +
"VALUES (1, '小明', '男', 18)";
stmt.executeUpdate(insertSQL);
String updateSQL = "UPDATE student " +
"SET age = 15 WHERE id = 1";
stmt.executeUpdate(updateSQL);
String deleteSQL = "delete FROM student where id = 1";
stmt.executeUpdate(deleteSQL);
conn.commit();
} catch (SQLException e) {
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
### B
```java
import java.sql.*;
public class Transaction {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
try {
String insertSQL = "INSERT INTO student " +
"VALUES (1, '小明', '男', 18)";
stmt.executeUpdate(insertSQL);
String updateSQL = "UPDATE student " +
"SET age = 15 WHERE id = 1";
stmt.executeUpdate(updateSQL);
String deleteSQL = "delete FROM student where id = 1";
stmt.executeUpdate(deleteSQL);
} catch (SQLException e) {
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
### C
```java
import java.sql.*;
public class Transaction {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.126.76:5432/ask_classifier", "csdn", "csdn");
Statement stmt = conn.createStatement();
conn.setAutoCommit(false);
String insertSQL = "INSERT INTO student " +
"VALUES (1, '小明', '男', 18)";
stmt.executeUpdate(insertSQL);
String updateSQL = "UPDATE student " +
"SET age = 15 WHERE id = 1";
stmt.executeUpdate(updateSQL);
String deleteSQL = "delete FROM student where id = 1";
stmt.executeUpdate(deleteSQL);
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
```
......@@ -98,6 +98,6 @@
}
}
],
"export": [],
"export": ["PSQLConnection.json", "CRUD.json", "Transaction.json"],
"title": "数据库操作"
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册