java mysql 链接例子

import java.sql.*;

public class sql {

	public static void main(String[] args) {
		System.out.print("HelloWorld!");
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Connection conn = null;
		try {
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery("select * from userinfo where userName='sun' and passWord='123'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(rs.next())
				{	
					System.out.print("成功登陆");
				}
			else{
					System.out.print("密码或用户名错误");
				}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}


你可能感兴趣的:(mysql,java)