2020/10/26

1.

在数据库配置好后

package DBUtil;


import java.sql.*;


public class DBUtil {

	public static String db_url = "jdbc:mysql://localhost:3306/text111?serverTimezone=GMT%2B8&useSSL=false";
	public static String db_user = "root";
	public static String db_pass = "zgq727";

	public static Connection getConn () {
		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(db_url, db_user,db_pass);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}//end getConn

	public static void close (Statement state, Connection conn) {
		if (state != null) {
			try {
				state.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void close (ResultSet rs, Statement state, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (state != null) {
			try {
				state.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws SQLException {
		Connection conn = getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		String sql ="select * from text2";
		pstmt = conn.prepareStatement(sql);
		rs = pstmt.executeQuery();
		if(rs.next()){
			System.out.println("连接成功");
		}else{
			System.out.println("连接失败");
		}
	}
}

 在建立java,这个程序用来连接数据库

 

public static String db_url = "jdbc:mysql://localhost:3306/text111?serverTimezone=GMT%2B8&useSSL=false";
	public static String db_user = "root";
	public static String db_pass = "zgq727";


使用该语句来连接数据库

二.是不是每个表都要写一次

三.web开发

 

posted @ 2020-10-26 23:25  小强哥in  阅读(28)  评论(0)    收藏  举报