连接数据库(整体代码在文末)
//要连接的数据库url,注意:此处连接的应该是服务器上的MySQl的地址
String url = "jdbc:mysql://192.168.3.5:3306/liaotian?useUnicode=true&characterEncoding=utf8";
//连接数据库使用的用户名
String userName = "root";
//连接的数据库时使用的密码
String password = "123456";
Connection connection = null;
try {
//1、加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
//2、获取与数据库的连接
connection = DriverManager.getConnection(url, userName, password);
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
增
//3.sql添加数据语句
String sql = "INSERT INTO user (zhanghao, possword) VALUES ( ?, ?)";
//4.获取用于向数据库发送sql语句的ps
PreparedStatement ps = connection.prepareStatement(sql);
//获取输入框的数据 添加到mysql数据库
ps.setString(1, "112233");
ps.setString(2, "223344");
int n = ps.executeUpdate();//更新数据库
System.out.println("添加:"+n);
删
//预编译
String sql = "delete from user where zhanghao = ? ";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,"369");
int n = ps.executeUpdate();
System.out.println("删除:"+n);
改
String sql="update user set possword = ? where zhanghao= ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,"258888888");
ps.setString(2,"258");
int n = ps.executeUpdate();
System.out.println("修改:"+n);
查
String sql = "select * from user ";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
String zhanghao = resultSet.getString(1);
String password = resultSet.getString(2);
System.out.println("查询:"+ zhanghao +"\n"+"密码:"+password);
}
整体代码
//要连接的数据库url,注意:此处连接的应该是服务器上的MySQl的地址
public static String url = "jdbc:mysql://192.168.3.5:3306/liaotian?useUnicode=true&characterEncoding=utf8";
//连接数据库使用的用户名
public static String userName = "root";
//连接的数据库时使用的密码
public static String password = "123456";
public static void main(String[] args) {
Connection connection = null;
//输入数字
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
System.out.println("输入:"+i);
try {
//1、加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
//2、获取与数据库的连接
connection = DriverManager.getConnection(url, userName, password);
/**
* 增
*/
if(i == 1){
//3.sql添加数据语句
String sql = "INSERT INTO user (zhanghao, possword) VALUES ( ?, ?)";
//4.获取用于向数据库发送sql语句的ps
PreparedStatement ps = connection.prepareStatement(sql);
//获取输入框的数据 添加到mysql数据库
ps.setString(1, "112233");
ps.setString(2, "223344");
int n = ps.executeUpdate();//更新数据库
System.out.println("添加:"+n);
}
/**
* 删
*/
else if(i == 2){
//预编译
String sql = "delete from user where zhanghao = ? ";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,"369");
int n = ps.executeUpdate();
System.out.println("删除:"+n);
}
/**
* 改
*/
else if(i == 3){
String sql="update user set possword = ? where zhanghao= ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,"258888888");
ps.setString(2,"258");
int n = ps.executeUpdate();
System.out.println("修改:"+n);
}
/**
* 查
*/
else {
String sql = "select * from user ";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()){
String zhanghao = resultSet.getString(1);
String password = resultSet.getString(2);
System.out.println("查询:"+ zhanghao +"\n"+"密码:"+password);
}
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
记得加载驱动哦