IDea快捷键
IDEA快捷键 查看网址 https://blog.youkuaiyun.com/weixin_42189233/article/details/80566249
sout+Tab 可生成print样板代码
fori + Tab 生成for 代码
ctrl + alt + V 可以自动导入变量定义 new String() 之类的
ctrl + alt+T 可选择代码块 try +catch 之类的
ctrl + D 复制行
ctrl + x 删除行
shift+ f6 重命名(文件
ctrl+ alt + f 变成成员变量
JDBC
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection("jdbc:mysql://localhost/ningda?user=root&password=root");
System.out.println("连接成功");
//3.写sql
String sql = "select * from tb_user";
//4.写statement对象
preparedStatement = connection.prepareStatement(sql);
//5.执行sql
resultSet = preparedStatement.executeQuery();
// 如果执行 增 改 删 则
// int affect = preparedStatement.executeQuery();
//然后再关闭相应的连接
//6.处理结果集
while(resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
}catch (Exception e){
e.toString();
}finally {
//7.关闭连接
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
JDBC工具类
import java.sql.*;
public class MyUtil {
public static int Update(String sql){
Connection connection = null;
PreparedStatement preparedStatement = null;
int affect = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/ningda?user=root&password=root");
preparedStatement = connection.prepareStatement(sql);
affect = preparedStatement.executeUpdate();
System.out.println("影响"+affect+"行");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
connection.close();
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return affect;
}
public static ResultSet Query(String sql){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/ningda?user=root&password=root");
System.out.println("连接成功");
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return resultSet;
}
}