javaweb

一.学习的内容
1.什么是JDBC
是java数据库连接,它是一套用于执行SQL语句的JavaAPI。
2,JDBC常用的API
(1)Driver接口;(2)DriverManger接口;(3)Connection接口;(4)Statement接口;(5)PreparedStatement接口;(6)ResultSet接口
3.编写代码如下:
(1)

package cn.itcast;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class example1 {
	public static void main(String[] args) throws SQLException{
		Statement stmt = null;
		ResultSet rs = null;
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC";
			String username = "root";
			String password = "123456";
			conn = DriverManager.getConnection(url, username, password);
			stmt = conn.createStatement();
			String sql = "select * from users";
			rs = stmt.executeQuery(sql);
			System.out.println("id | name | password | email | birthday");
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				String psw = rs.getString("password");
				String email = rs.getString("email");
				Date birthday = rs.getDate("birthday");
				System.out.println(id+"|"+name+"|"+psw+"|"+email+"|"+birthday);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			if (rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				rs = null;
			}
			if (stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				stmt = null;
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				conn = null;
			}
		}
	}
}


(2)

package cn.itcast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class example2 {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC";
			String username = "root";
			String password = "123456";
			conn = DriverManager.getConnection(url, username, password);
			String sql = "select * from users";
			stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			ResultSet rs = stmt.executeQuery(sql);
			System.out.print("第2条数据的name值为:");
			rs.absolute(2);
			System.out.println(rs.getString("name"));
			System.out.print("第1条数据的name值为:");
			rs.beforeFirst();
			rs.next();
			System.out.println(rs.getString("name"));
			System.out.print("第4条数据的name值为:");
			rs.afterLast();
			rs.previous();
			System.out.println(rs.getString("name"));
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				stmt = null;
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				conn = null;
			}
		}
	}

}



(3)

package cn.itcast;
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 example3 {
	public static void main(String[] args) throws SQLException{
		Connection conn = null;
		PreparedStatement preStmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC";
			String username = "root";
			String password = "123456";
			conn = DriverManager.getConnection(url, username, password);
			String sql = "INsERT INTO users(name,password,email,birthday)"
			            +"VALUES(?,?,?,?)";
			preStmt = conn.prepareStatement(sql);
			preStmt.setString(1, "zl");
			preStmt.setString(2, "123456");
			preStmt.setString(3, "[email protected]");
			preStmt.setString(4, "1789-12-23");
			preStmt.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			if (preStmt!=null) {
				try {
					preStmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				preStmt = null;
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				conn = null;
			}
		}
	}
}


(4)

package cn.itcast.dao;

import java.sql.Date;

public class User {

	public void setId(int int1) {
		// TODO Auto-generated method stub
		
	}

	public void setUsername(String string) {
		// TODO Auto-generated method stub
		
	}

	public void setPassword(String string) {
		// TODO Auto-generated method stub
		
	}

	public void setEmail(String string) {
		// TODO Auto-generated method stub
		
	}

	public void setBirthday(Date date) {
		// TODO Auto-generated method stub
		
	}

}

(5)

package cn.itcast.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
	public static Connection getConnection() throws SQLException,ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC";
		String username = "root";
		String password = "123456";
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}
	public static void release(Statement stmt,Connection conn) {
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt=null;
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn=null;
		}
		
	}
	public static void release(ResultSet rs,Statement stmt,Connection conn) {
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=null;
		}
		release(stmt, conn);
	}
	public static Connection getCon() {
		// TODO Auto-generated method stub
		return null;
	}
	public static void realse(Object object, Statement stmt, Connection conn) {
		// TODO Auto-generated method stub
		
	}

}

(6)

package cn.itcast.dao;

import java.sql.Connection;
import java.sql.Statement;

import javax.management.RuntimeErrorException;

import cn.itcast.domain.User;
import cn.itcast.utils.JDBCUtils;

public class usesdao {
private boolean ture;

public boolean insert(User user) {
	Connection conn = null;
	Statement stmt = null;
	try {
		conn=JDBCUtils.getCon();
		
		stmt=conn.createStatement();
		
		java.util.Date brithday=user.getBirthday();
		String sqlBirthDay=String.format("%tF", "birthday");
		String sql="insert into user(id,name,password,email,birthday)"+"values("
		+user.getId()+"','"
		+user.getUsername()+"','"
		+user.getPassword()+"','"
		+user.getEmail()+"','"
		+sqlBirthDay+"','"
		+")";
		System.out.println(sql);
		int row =stmt.executeUpdate(sql);
		if(row>0) {
			return ture;
        }
	}catch(Exception e) {
		throw  new RuntimeException(e);
	}finally {
		JDBCUtils.realse(null,stmt,conn);
	}
	return false;
	
}

}

(7)

package cn.itcast.jdbc.excample;
import java.util.Date;
import cn.itcast.dao.UsersDao;
import cn.itcast.domain.User;
public class JdbcInsertTest {
	public static void main(String[] args) {
		UsersDao dao = new UsersDao();
		User user = new User();
		user.setId(5);
		user.setUsername("h1");
		user.setPassword("123");
		user.setEmail("[email protected]");
		user.setBirthDay(new Date());
		boolean flag= dao.insert(user);
		
		
	}

}


(8)

package cn.itcast.jdbc.excample;

import cn.itcast.dao.UsersDao;
import cn.itcast.domain.User;
public class FindUserByldTest {
	public static void main(String[] args) {
		UsersDao usersDao = new UsersDao();
		User user = usersDao.find(1);
		System.out.println("id为1的User对象的name值为:"+user.getUsername());
		
	}
}


效果图如下

javaweb_第1张图片

javaweb_第2张图片
javaweb_第3张图片

你可能感兴趣的:(javaweb,sql,mysql,数据库)