第一次写博客记录下今天终于把Java基础告一段落
自己由于(大学)学校资源有限,大不了自己靠自己学了下java,过程中随着学习的深入也觉得java的魅力所在。上学期也学JAVA,这学期又自己重温了一下知识,也算是复习吧!
(自己萌新一个,要是有什么好的建议请多多指教 // 手动谢谢)
数据库:MYSQL
JDK:1.8
IDE工具:IDEA
四个文件:
- 主文件 ——demo1
- 自己创建的连接对象类:连接类_JDBCutils
3. 配置单:yinhang.properties(新手可以Ctrl+c嫖走,注意是.properties文件 - 数据库文件:yinhang
文件1(里面有附带解释)
package day40_JDBC银行转账案例;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class demo1 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement ps = null;
try {
connection = 连接类_JDBCutils.getconnection();
connection.setAutoCommit(false);
ps = connection.prepareStatement
("update account set balance = balance - ? where name=?");
ps.setInt(1, 500);
ps.setString(2, "wlw");
ps.executeUpdate();
ps = connection.prepareStatement
("update account set balance = balance + ? where name=?");
ps.setInt(1, 500);
ps.setString(2, "yjj");
ps.executeUpdate();
connection.commit();
System.out.println("转账成功");
} catch (Exception e) {
e.printStackTrace();
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
System.out.println("转账失败");
}
finally {
连接类_JDBCutils.close(connection,ps);
}
}
}
文件2
package day40_JDBC银行转账案例;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class 连接类_JDBCutils {
private static String user = null;
private static String passwd = null;
private static String driver = null;
private static String url = null;
static {
Properties properties = new Properties();
try {
properties.load(new FileReader("src/yinhang.properties"));
user = properties.getProperty("user");
url = properties.getProperty("url");
passwd = properties.getProperty("passwd");
driver = properties.getProperty("driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public static Connection getconnection() throws SQLException {
return DriverManager.getConnection(url, user, passwd);
}
public static void close(Connection connection, Statement statement) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
文件3
user=root
passwd=123456
url=jdbc:mysql://localhost:3306/yinhang
driver=com.mysql.cj.jdbc.Driver
文件4
create database yinhang;
use yinhang;
CREATE TABLE account (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(10),
balance int
);
INSERT INTO account (NAME, balance) VALUES ('wlw', 1000), ('yjj', 1000);
select * from account;
(自己萌新一个,要是有什么好的建议请多多指教 // 手动谢谢)
种一棵树的最好时间是十年前和现在!
剩下的就每天多学习,多看书,提升自己,Enjoy Typing Code