Java Study
报错
java.io使用相对路径读取文件找不到文件
解决办法:
检查run->edit Configuration->work directory中的默认路径
IDEA中Spring配置错误
class path resource [applicationContext.xml] cannot be opened because it does not exist
解决办法1:
1.把 .class 也放进 src 目录中,
注意:不能直接把 applicationContext.xml 移至target目录下,因为 .xml 配置文件运行时也需要在 .java 文件中获取属性信息
2.修改 Output Path 到 src 目录下即可:
点击 File -> Project Structure(或快捷键 Ctrl+Alt+Shift+S)
解决方法2:
1.在src/main目录下新建一个文件夹,建议取名resource
2.点击 File -> Project Structure(或快捷键 Ctrl+Alt+Shift+S)
3.点击 Modules -> Sources 将新建的文件夹标记为 Resources
4.将applicationContext.xml放入resources文件夹
5.重新编译运行即可
注解
定义注解
Java语言使用@interface语法来定义注解(Annotation),它的格式如下:
public @interface Report {
int type() default 0;
String level() default "info";
String value() default "";
}
数组
public class Main{
public static void main(String[] args) {
int[][] arr = {
{1, 2},
{3, 4},
{5, 6}};
//System.out.println(arr[2][1]);
for (int[] i : arr) {
for (int j : i) {
System.out.println(j);
}
}
}
}
数据库连接
import java.sql.*;
public class TestJdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//注册JDBC驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
//打开一个连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/world?&userSSL=false",
"root",
"3033715900");
//执行一个查询
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select *from city where CountryCode='AFG'");
//ResultSet rs = stmt.executeUpdate(sql); UPDATE,INSERT或DELETE语句
// 从结果集中提取数据
while (rs.next()) {
//Retrieve by column name
int id = rs.getInt("ID");
String name = rs.getString("Name");
//Display values
System.out.print("ID: " + id);
System.out.println(", Name: " + name);
}
conn.close();
}
}
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment