package com.util;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBTest {
//查询
public static void testconnection(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hunqing";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection(url, "root", "123456");
System.out.println("加载驱动成功");
if(conn != null){
System.out.println("连接成功");
System.out.println("查询数据:");
String SQL = "select * from user";
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery(SQL);
while(resultset.next()){
int id = resultset.getInt("userId");
String name = resultset.getString("userName");
String Pw = resultset.getString("userPw");
System.out.print(id+",");
System.out.print(name+",");
System.out.println(Pw);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//增加
public static void testconnection1(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hunqing";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection(url, "root", "123456");
System.out.println("加载驱动成功");
if(conn != null){
System.out.println("连接成功");
try {
System.out.println("添加数据:");
Statement statement = conn.createStatement();
String SQL = "insert into user(userName,userPw) values ('xiaozhou','123')";
int num = statement.executeUpdate(SQL);
System.out.println("成功插入" + num + "数据");
} catch (Exception e) {
System.out.println("添加数据失败");
}
System.out.println("查询数据:");
String SQL1 = "select * from user";
Statement statement1 = conn.createStatement();
ResultSet resultset1 = statement1.executeQuery(SQL1);
while(resultset1.next()){
int id = resultset1.getInt("userId");
String name = resultset1.getString("userName");
System.out.print(id+",");
System.out.println(name+",");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//修改
public static void testconnection2(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hunqing";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection(url, "root", "123456");
System.out.println("加载驱动成功");
if(conn != null){
System.out.println("连接成功");
try {
System.out.print("修改数据:");
Statement statement = conn.createStatement();
String SQL = "UPDATE user SET userName = 'xiaoxiao' WHERE userId = 2";
statement.executeUpdate(SQL);
System.out.println("成功修改userId = 1数据");
statement.close();
} catch (Exception e) {
System.out.println("修改数据失败");
}
System.out.println("查询数据:");
String SQL1 = "select * from user";
Statement statement1 = conn.createStatement();
ResultSet resultset1 = statement1.executeQuery(SQL1);
while(resultset1.next()){
int id = resultset1.getInt("userId");
String name = resultset1.getString("userName");
System.out.print(id+",");
System.out.println(name+",");
}
resultset1.close();
statement1.close();
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除
public static void testconnection3(){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hunqing";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection(url, "root", "123456");
System.out.println("加载驱动成功");
if(conn != null){
System.out.println("连接成功");
try {
System.out.print("删除数据:");
Statement statement = conn.createStatement();
String SQL = "delete from user WHERE userId = 1";
statement.executeUpdate(SQL);
System.out.println("成功删除userId = 1数据");
statement.close();
} catch (Exception e) {
System.out.println("删除数据失败");
}
System.out.println("查询数据:");
String SQL1 = "select * from user";
Statement statement1 = conn.createStatement();
ResultSet resultset1 = statement1.executeQuery(SQL1);
while(resultset1.next()){
int id = resultset1.getInt("userId");
String name = resultset1.getString("userName");
System.out.print(id+",");
System.out.println(name+",");
}
resultset1.close();
statement1.close();
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//登陆
public static void testconnection4(String name,String password){
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/hunqing";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
Connection conn=DriverManager.getConnection(url, "root", "123456");
System.out.println("加载驱动成功");
if(conn != null){
System.out.println("连接成功");
System.out.println("查询数据:");
String SQL = "select * from user where userName =? and userPw=?";
PreparedStatement preparedstatement = conn.prepareStatement(SQL);
preparedstatement.setString(1,name);
preparedstatement.setString(2,password);
ResultSet resultset = preparedstatement.executeQuery();
while(resultset.next()){
name = resultset.getString("userName");
System.out.print("登陆成功");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String [] args) {
testconnection4("xiaoxiao","222");
}
}