- 需要导入jar包: mysql-connector-java-5.1.42-bin.jar
- JDBC
package work;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class database {
public static void fun() throws ClassNotFoundException, SQLException{
System.out.println("****************");
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/javawork";
String user="sa";
String password="123456";
Class.forName(driver);
Connection conn=DriverManager.getConnection(url, user, password);
System.out.println(conn);
Statement str=conn.createStatement();
str.executeUpdate(sql);
str.close();
conn.close();
}
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
fun();
}
}
- JDBCUNIT
package work;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcUnit {
/**
* 获得数据库连接
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/javawork","zm","123456");
}
/**
* 关闭数据库连接
* @param resultSet
* @param statement
* @param connection
*/
public static void close(ResultSet resultSet,Statement statement ,Connection connection){
if(resultSet!=null){
try {
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
resultSet=null;
}
}
if(statement!=null){
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
statement=null;
}
}
if(connection!=null){
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
connection=null;
}
}
}
}
- JDBCFUN
package work;
//import java.awt.List;
import java.sql.Connection;
//import java.sql.DriverManager;
import java.sql.ResultSet;
//import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class jdbcfun {
//
// public static void main(String[] args) {
// userbean users=new userbean();
// users.setName("123");
// users.setPassword("2");
// System.out.println(ispassword(users));
//
// }
//
public static boolean finduser(String username){
Boolean bool=false;
PreparedStatement ps=null;
Connection conn=null;
Statement str=null;
ResultSet re=null;
try{
conn= jdbcUnit.getConnection();
str=conn.createStatement();
String sql="select * from user where name=?;";
ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setString(1, username);
re=ps.executeQuery();
System.out.println(username);
if(!re.next())
{
bool=false;
}
else
{
bool=true;
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
jdbcUnit.close(re, str, conn);
}
return bool;
}
public static void insert(userbean users) {
Connection conn=null;
Statement str=null;
ResultSet re=null;
PreparedStatement ps=null;
try{
conn= jdbcUnit.getConnection();
str=conn.createStatement();
String sql="insert into user (name,password,mail)values(?,?,?);";
ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setString(1, users.getName());
ps.setString(2, users.getPassword());
ps.setString(3, users.getEmail());
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally{
jdbcUnit.close(re, str, conn);
}
}
public static boolean ispassword(userbean users){
Boolean bool=false;
PreparedStatement ps=null;
Connection conn=null;
Statement str=null;
ResultSet re=null;
try{
conn= jdbcUnit.getConnection();
str=conn.createStatement();
String sql="select password from user where name=?;";
ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setString(1, users.getName());
System.out.println( users.getName());
re=ps.executeQuery();
if(!re.next())
{
bool=false;
}
else
{
re=ps.executeQuery();
while(re.next()){
String password1=re.getString("password");
String password2=users.getPassword();
if(password1.equals(password2)){
bool=true;
}
else
{
bool=false;
}
}
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
jdbcUnit.close(re, str, conn);
}
return bool;
}
public static void update(userbean users)
{
Connection conn=null;
Statement str=null;
ResultSet re=null;
PreparedStatement ps=null;
try{
conn= jdbcUnit.getConnection();
str=conn.createStatement();
String sql="update user set password=? where name=?;";
ps=(PreparedStatement) conn.prepareStatement(sql);
ps.setString(2, users.getName());
ps.setString(1, users.getPassword());
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally{
jdbcUnit.close(re, str, conn);
}
}
public static void findall(){
//List list=null;
Connection conn=null;
Statement str=null;
ResultSet re=null;
try{
conn= jdbcUnit.getConnection();
str=conn.createStatement();
String sql="insert into user values(users.name,users.password,users.email);";
re=str.executeQuery(sql);
while (re.next()) {
System.out.println(re.getString("name"));
System.out.println(re.getString("password"));
System.out.println(re.getString("mail"));
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
jdbcUnit.close(re, str, conn);
}
//return list;
}
}