服务端数据库(MySQL)

当我们做出一个软件的时候,如果想让用户有属于自己账号密码,这时候就需要用服务端来完成,而我们服务端的数据库一般就采用MySQL,服务端的开发环境我使用的是MyEclipse。

如果我们想要对数据库进行操作,就必须先获取连接。而要获取连接,我们需要4个非常重要的字符串,首先我们先在MyEclipse新建一个项目,新建一个后缀为Properties的文件,Properties就是集合中一种,该文件会以键值对的方式存储。前面说的4个重要的字符串就是要存储到这个文件中的值,至于键就看我们的喜好了,这是我的Properties文件


第一行字符串用于加载MySQL的JDBC驱动,当然我们要加载JDBC驱动,要在WebRoot文件下的WEB-INF-->lib文件放入一个启动JDBC的文件,下面附上下载链接

http://download.csdn.net/detail/ning109314/2543435

第二行字符串为服务端的主机项目地址,localhost为主机,3306为端口号,?后面为使用的字符集及编码集。主机可以用当前ip地址替代

第三第四就是注册数据库的时候使用的账号跟密码,照着你注册时输入即可


做完这些准备工作,我们就可以获取连接了。

下面是获取连接数据库,和关闭连接的方法

package com.zx.news.dbutil;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class DBUtil {
	
	/**
	 * 获取连接
	 * 要获取连接才能对数据库进行操作
	 * @throws IOException 
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException{
		//util包下的
		Properties properties = new Properties();
		DBUtil dbUtil = new DBUtil();
		//获取流
		Class<? extends DBUtil> class1 = dbUtil.getClass();
		ClassLoader classLoader = class1.getClassLoader();//获取流的加载对象
		InputStream inStream = classLoader.getResourceAsStream("com/zx/news/dbutil/DbConfig.Properties");//以流的形式获取资源
//		FileInputStream inStream = new FileInputStream("/News/src/com/zx/news/dbutil/DbConfig.Properties");
		properties.load(inStream);//读取流
		//获取该文件下的值
		String driver = properties.getProperty("driver");
		String url = properties.getProperty("url");
		String userName = properties.getProperty("username");
		String password = properties.getProperty("password");
		//加载MySQL的JDBC的驱动
		Class.forName(driver);
		//获取连接
		Connection connection = DriverManager.getConnection(url, userName, password);
		return connection;
	}
	
	/**
	 * 关闭连接
	 */
	public static void closeConnection(Connection connection) {
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}

当然,我们获取连接的目的是为了对数据库进行操作。对数据的操作就要用到SQL语句了,无外乎增、删、改、查。当然如果用户注册账号的话我们还需要判断这个账户是否存在,登陆需要判断账号或者密码是否正确。这些方法我们都应该提供,如下:

package com.zx.news.dbutil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AccountManager {
	Connection connection;

	public AccountManager() throws IOException, ClassNotFoundException,
			SQLException {
		// 再构造方法中获取连接的对象
		connection = DBUtil.getConnection();
	}

	/**
	 * 添加数据的方法
	 * 
	 * @return ture添加成功,反之失败
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public boolean register(String userName, String password) throws SQLException {

		String sql = "insert into account(username,password) value('"
				+ userName + "','" + password + "')";
		return update(sql);
	}

	/**
	 * 删除数据的方法
	 * 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public boolean delete(int id, String userName) throws SQLException,
			IOException, ClassNotFoundException {

		String sql = "delete from account where id ='" + id + "'or username ='"
				+ userName + "'";
		return update(sql);
	}
	
	/**
	 * 增、删、改
	 * @throws SQLException 
	 */
	public boolean update(String sql) throws SQLException {
		Statement statement = connection.createStatement();
		int update = statement.executeUpdate(sql);
		if (update == 0) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * 查找
	 * @throws SQLException 
	 */
	public void select() throws SQLException {
		Statement statement = connection.createStatement();
		String sql = "select * from account ";
		//返回的是ResultSet对象,与游标相似
		ResultSet resultSet = statement.executeQuery(sql);
//		resultSet.moveToCurrentRow();//当前行
//		resultSet.moveToInsertRow();//插入行?
		
		while (resultSet.next()/*若还有下一行,则返回true,并进入下一行*/) {
//			String userName = resultSet.getString("username");//根据列名查询
//			String password = resultSet.getString("password");
		}
	}
	
	/**
	 * 查询用户是否存在
	 * @param userName
	 * @return true-->存在 false-->不存在
	 * @throws SQLException
	 */
	public boolean userNmaeExist(String userName) throws SQLException {
		Statement statement = connection.createStatement();
		String sql = "select * from account where username ='"+userName+"'";
		ResultSet resultSet = statement.executeQuery(sql);
		if (resultSet.next()) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 查询用户跟密码是否正确
	 * @throws SQLException 
	 */
	public boolean accountExist(String userName, String password) throws SQLException {
		Statement statement = connection.createStatement();
		String sql = "select * from account where username = '"+userName+"'and password ='"+password+"'";
		ResultSet resultSet = statement.executeQuery(sql);
		if (resultSet.next()) {
			return true;
		}else {
			return false;
		}
	}

}


千年服务端架设MySQL是一个相对简单但又需要谨慎处理的过程。首先,我们需要在服务器上安装MySQL数据库软件。这可以通过下载并安装MySQL软件包来完成。一旦安装完成,我们就需要对MySQL进行一些基本的配置。 首先,我们需要设置MySQL的root用户的密码。这是一个非常重要的步骤,因为它可以保护我们的数据库免受未经授权的访问。我们可以使用MySQL的命令行界面进入MySQL,并使用ALTER USER语句来更改root用户的密码。 接下来,我们应该创建一个新的数据库和表格。我们可以使用CREATE DATABASE语句创建一个新的数据库,然后使用CREATE TABLE语句创建表格。在创建表格时,我们需要指定每个字段的名称、数据类型和约束条件。 一旦数据库和表格创建完成,我们就可以开始向表格中插入数据了。我们可以使用INSERT INTO语句将数据插入表格中。我们需要指定要插入的数据的值和字段的顺序。 除了插入数据,我们还可以使用SELECT语句从表格中检索数据。SELECT语句可以根据特定的条件过滤数据,并返回满足条件的记录。 此外,我们还可以使用UPDATE语句来更新表格中的数据,使用DELETE语句来删除表格中的数据。 最后,我们还应该定期备份MySQL数据库,以防止数据丢失。我们可以使用mysqldump命令来备份整个数据库,或使用SELECT INTO OUTFILE语句将数据导出到CSV文件中。 总之,千年服务端架设MySQL需要安装、配置、创建数据库和表格、插入和检索数据、备份等一系列步骤。这些步骤需要谨慎处理,以确保数据库的稳定性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值