admin

JavaSE
字符串字符串类String字符串缓冲类StringBuffer 线程安全StringBuilder 线程不安全跨平...
扫描右侧二维码阅读全文
17
2019/01

JavaSE

字符串

字符串类

String

字符串缓冲类

StringBuffer 线程安全
StringBuilder 线程不安全

跨平台相关

换行

Windows:\r\n
Linux:\n

File

pathSeparator

与系统有关的路径分隔符:Win:; Linux:

separator

系统有关的默认名称分隔符:Win\ Linux/

IO流

InputStream

FileInputStream
字节缓冲输入流BufferedInputStream

OuputStream

FileOutputStream
字符缓冲输出流BufferedOuputStream

Writer

字符转换流OutputStreamWriter
FileWriter
字符缓冲输出流BufferedWriter

Reader

字符转换流InputStreamReader
FileReader
字符缓冲输入流BufferedReader

MySQL操作

方式一原生连接MySQL

 //注册驱动
 Class.forName("com.mysql.jdbc.Driver");
 //获取连接对象
 String url = "jdbc:mysql://localhost:3306/test";
 String user = "root";
 String password = "root";
 Connection con = DriverManager.getConnection(url, user, password);
 //获取执行SQL对象
 Statement stat = con.createStatement();
//拼写插入SQL
String sql = "INSERT INTO user(username,password)VALUES('test','test123')";
int i = stat.executeUpdate(sql);
System.out.println(i);
//拼写查询的SQL
String sql = "SELECT * FROM `user`";
ResultSet resultSet = stat.executeQuery(sql);
while(resultSet.next()){
String name = resultSet.getString("username");
String pass = resultSet.getString("password");
System.out.println(name+"..."+pass);
rs.close();
stat.close();
con.close();

}

遇到的问题:

execute(sql)返回的逻辑值:查询返回true,插入或更新返回false

方式二 预编译方式,防止SQL注入

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "root");

//预编译SQL
String sql = "SELECT * FROM user WHERE id = ?";
PreparedStatement pst = con.prepareStatement(sql);
//将占位符填充
pst.setInt(1,1);

ResultSet rs = pst.executeQuery();
while(rs.next()){
String user = rs.getString("username");
String pass = rs.getString("password");
System.out.println(user+"..."+pass);
}

rs.close();
pst.close();
con.close();

预编译更新操作

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "root");
String sql = "UPDATE user SET password=? WHERE username=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,"xiugaile");
pst.setString(2,"test");
int i = pst.executeUpdate();
System.out.println(i);
最后修改:2019 年 01 月 23 日 07 : 16 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论