• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

连接数据库模版

mysql 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

package com.mingxin.util;import java.sql.Connection;import java.sql.DriverM

本文来源gao!%daima.com搞$代*!码9网(

anager;import java.sql.SQLException;import com.mysql.jdbc.ResultSet;import com.mysql.jdbc.Statement;public class ConnectionUtils {private static String driver = com

package com.mingxin.util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import com.mysql.jdbc.ResultSet;import com.mysql.jdbc.Statement;public class ConnectionUtils {	private static String driver = "com.mysql.jdbc.Driver";	private static String url = "jdbc:mysql://127.0.0.1:3306/db_mx";	private static String user = "root";	private static String password = "123456";	private static Connection conn = null;		static{		try {			Class.forName(driver);		} catch (ClassNotFoundException e) {			System.out.println("驱动加载失败..");			e.printStackTrace();		}	}		public static Connection getConnection(){		try {			conn = DriverManager.getConnection(url, user, password);			if(!conn.isClosed()){				System.out.println("Successed connecting to the Database...");			}		} catch (SQLException e) {			System.out.println("数据库连接失败...");			e.printStackTrace();		}		return conn;	}		public static void main(String[] args) {//		getConnection();		try {			Statement statement = (Statement) conn.createStatement();			String sql = "select * from user";			ResultSet rs = (ResultSet) statement.executeQuery(sql);			String email = null;			String name = null;			String phone = null;			while(rs.next()){				email = rs.getString("u_email");				name = rs.getString("u_name");				phone = rs.getString("u_phone");				System.out.println("u_email=" + email + " ___u_name=" + name + " ____phone=" + phone);			}		} catch (SQLException e) {			e.printStackTrace();		}	}}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:连接数据库模版
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址