第六章 JDBC
程序员文章站
2022-03-07 11:34:00
...
1,在MySQL创建一个student表,添加数据,然后在控制台输出目前表中记录的总数
package zuoye.bdqn.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Student {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/zuoye?useUnicode=true&characterEncoding=utf-8","root","520");
pstmt=conn.prepareStatement("insert into student (id,name) values (?,?)");
pstmt.setInt(1, 1);
pstmt.setString(2,"黄志豪");
pstmt.executeUpdate();
System.out.println("插入成功!");
rs=pstmt.executeQuery("select count(*)from student");
while(rs.next()) {
System.out.println("表*有"+rs.getInt(1)+"条记录");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2,查询student表所有记录,并在控制台输出数据
package zuoye.bdqn.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class student1 {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zuoye","root","520");
st= conn.createStatement();
rs=st.executeQuery("select * from student");
while(rs.next()) {
System.out.println("序号:"+rs.getInt("id")+"姓名:"+rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3,宠物主人根据控制台输入用户名和密码,如果输入正确,输出用户登录成功,否则输出用户登录失败
package zuoye.bdqn.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class student2 {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Scanner in=new Scanner(System.in);
System.out.println("\t宠物主人登录");
System.out.println("请输入姓名:");
String name=in.next();
System.out.println("请输入密码:");
String password=in.next();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/zuoye?useUnicode=true&characterEncoding=utf-8","root","520");
String sql="select *from master where name=?and password=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
rs=pstmt.executeQuery();
if(rs.next()) {
System.out.println("登录成功!");
}else {
System.out.println("登录失败!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上一篇: JDBC数据库连接池
下一篇: SQL中在查询结果中新增一列自定义的值
推荐阅读
-
封装-jdbc查询时间优化,判断主键是否重复的方法体简化怎么写
-
python编程快速上手第六章实践项目参考code
-
jdbc向Oracle数据库插入系统时间
-
flink-connector-jdbc.jar加入clickhouse驱动支持,并重新编译
-
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException解决
-
JDBC 入门(一)_MySQL
-
mysql-如何用JSTL JDBC MYSQL 实现增删查改?
-
数据库-异常:com.mysql.jdbc.exceptions.jdbc4.communicatio
-
JDBC 入门(一) - 开始
-
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException Ta