连接Mysql
来源:互联网 发布:s7200plc通讯端口设置 编辑:程序博客网 时间:2024/06/15 20:33
package com.lee.javaweb.mvc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LinkMysql {
public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/test";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123456";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from StudentTable";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println("姓名" + "\t" + "职称");
System.out.println("-----------------");
String job = null;
String id = null;
while(rs.next()){
//获取stuname这列数据
job = rs.getString("student_name");
//获取stuid这列数据
id = rs.getString("location");
//输出结果
System.out.println(id + "\t" + job);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LinkMysql {
public static void main(String[] args) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/test";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "123456";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
//2.创建statement类对象,用来执行SQL语句!!
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from StudentTable";
//3.ResultSet类,用来存放获取的结果集!!
ResultSet rs = statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println("姓名" + "\t" + "职称");
System.out.println("-----------------");
String job = null;
String id = null;
while(rs.next()){
//获取stuname这列数据
job = rs.getString("student_name");
//获取stuid这列数据
id = rs.getString("location");
//输出结果
System.out.println(id + "\t" + job);
}
rs.close();
con.close();
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取!!");
}
}
}
阅读全文
0 0
- MYSQl 连接
- 连接MySQL
- mysql连接
- MySQL连接
- 连接MySQl
- 连接mysql
- MySQL连接
- 连接MySQL
- MySql连接
- mysql连接
- mysql连接
- MySQL连接
- mysql 连接
- mysql连接
- MySql连接
- 连接mysql
- MySQL连接
- MySQL 连接
- Md5加密中文的坑
- 常用集合在多线程下的问题
- MySQL ERROR 1862 (HY000)
- Oracle一些特殊用法
- Leetcode 56. Merge Intervals
- 连接Mysql
- oracle-case
- FlV(H264+AAC)格式详细分析
- irq n: nobody cared (try booting with the "irqpoll" option)
- 为什么我还在用Windows?
- 微信小程序页面跳转时传参数
- Appium+Genymotion+robotframework+python系列一:appium环境配置
- 文章标题
- 【剑指Offer】面试题17:合并两个排序的链表