发表于: 2017-08-09 23:49:48
1 971
今天完成的任务
了解了关于sql的其它关键字
idbc中对数据库的操作均通过以下代码语句输入
Statement stmt = null;
stmt = conn.createStatement();
stmt.executeUpdate(sql);
sql即需要输入的语句
1)CREATE DATABASE 创建一个新数据库
2)DROP DATABASE删除一个数据库
3)CREATE TABLE table_name
(
column_name column_data_type,
column_name column_data_type,
column_name column_data_type ...);
范例
CREATE TABLE Employees(
id INT NOT NULL,
age INT NOT NULL,
first VARCHAR(255),
last VARCHAR(255),
PRIMARY KEY ( id ));
4)DROP TABLE table_name;删除表
5)INSERT INTO table_name VALUES (column1, column2, ...);插入一行新数据
6)SELECT column_name, column_name, ...
FROM table_name
WHERE conditions;
在名为table_name的表中读取名为conditions关键字的,column_name列数据
(WHERE后可以使用比较运算符)
7)UPDATE table_name
SET column_name = value, column_name = value, ...
WHERE conditions;
在名为table_name的表中将名为conditions关键字的,column_name列数据替换为输入值
(WHERE后可以使用比较运算符)
2.以下为今天写的
//STEP 1. Import required packages
// See more detail at http://www.yiibai.com/jdbc/
import java.sql.*;
public class InsertRecords {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/EMP?";
// Database credentials
static final String USER = "root";
static final String PASS = "123";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();
String sql = "INSERT INTO employees " +
"VALUES (100, '4', 'Li', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO employees " +
"VALUES (101, '3', 'Py', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO employees " +
"VALUES (102, '2', 'Ru', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO employees " +
"VALUES(103, '1', 'Ja', 28)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
现在有一个小问题,也就是如果我使用两个不同的sql语句导入同一个stmt.executeUpdate(sql);里面
stmt = conn.createStatement();//对 Statement执行sql语句,其实就是对这个执行的。。
String sql;
sql = "SELECT id, first, last, age FROM Employees";//查询数据的语法
ResultSet rs = stmt.executeQuery(sql);//把查询到的数值导入一个数组
sql = "INSERT INTO employees " + "VALUES (1770, '11', 'Li', 180)";
stmt.executeUpdate(sql);
会报java.sql.SQLException: Operation not allowed after ResultSet closed
但是
String sql = "INSERT INTO employees " +
"VALUES (100, '4', 'Li', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO employees " +
"VALUES (101, '3', 'Py', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO employees " +
"VALUES (102, '2', 'Ru', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO employees " +
"VALUES(103, '1', 'Ja', 28)";
stmt.executeUpdate(sql);
如果不写查询语句这样写就不会报错
不知道因为什么...
明天要做的事
听从师兄的建议,补习java基础,暂时放置任务1后面的内容
遇到的问题
如上
收获
1.对后面的学习计划更加的了解
2.掌握了sql的基本语法
评论