工具:eclipse

   MySQL5.7.17

   MySQL连接驱动:mysql-connector-java-5.1.43.jar

加载驱动:我是用MAVEN进行管理

数据库连接信息:

  数据库名称:wuwei

  数据包名称:Greeting

  端口号:3306

  用户名:root

  密码:******

将这些存放在database.properties文件中。

源代码:
 package hadoop.mysql;

 import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; /**
*
* @ClassName: Sql
* @Description: This program tests that the database and the JDBC driver are correctly configured
* @author ***
* @date 2017-9-4 下午11:27:22
*
*/
public class Sql { /**
*
* @Title: getConnection
* @Description: Gets a connection from the properties specified in the file database,properties * @throws IOException
* @throws SQLException
* @return Connection
*/
public static Connection getConnection ( ) throws IOException, SQLException
{
//创建一个Properties,并加载database.properties
Properties props = new Properties() ;
try ( InputStream in = Files.newInputStream(Paths.get("H://java//com.autwit.www//src//main//resources//database.properties")))
{
props.load( in ) ;
}
//驱动程序名
String drivers = props.getProperty( "jdbc.drivers" ) ;
if(drivers != null ) System.setProperty( "jdbc.drivers", drivers ) ;
//URL指向要访问的数据库名wuwei
String url = props.getProperty( "jdbc.url" ) ;
//数据库用户名
String username = props.getProperty( "jdbc.username" ) ;
//密码
String password = props.getProperty( "jdbc.password" ) ; return DriverManager.getConnection( url, username, password ) ;
}
/**
*
* @Title: runTest
* @Description: create a connect with MySql,Then executing C(create)R(read)U(Update)D(delete)
*
* @throws SQLException
* @throws IOException
* @return void
*/
public static void runTest() throws SQLException, IOException
{
//声明Connection对象
try( Connection con = getConnection() )
{
//创建statement类对象,用来执行SQL语句
Statement stat = con.createStatement( ) ;
stat.executeUpdate(" create table Greeting ( Message Char(20) )") ;
stat.executeUpdate( "Insert into Greeting values ('Hello world!' )") ;
//ResultSet类,用来存放获取的结果集!!
try (ResultSet rs = stat.executeQuery("select * from Greeting"))
{
/*
Notice :即使你十分确定能搜出记录,也不可以在没有rs.next()之前直接对rs进行取值。
这涉及到rs对象的存储方法。里面说白了就是指针。没next,指针根本没指向对应记录
*/
String message = "";
if(rs.next()){//或者while(rs.next())
message = rs.getString("Message");
if(message == null){
message = "";
}
System.out.println(message);
}
}
stat.executeUpdate("drop table Greeting") ;
}
} public static void main(String[] args) throws SQLException, IOException { runTest( ) ;
}
}

执行结果:

参考文献:1,http://www.cnblogs.com/centor/p/6142775.html

2,JAVA核心卷II

最新文章

  1. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q124-Q127)
  2. C语言中数组名作为参数进行函数传递
  3. Tomcat8启动报there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
  4. c#网络通信框架networkcomms内核解析之九 自定义处理方法的运行机制
  5. kettle job通过javascript进行循环控制
  6. 在navgationController中添加UISegmentedControl
  7. .net 实例化对象
  8. IIS Express 终极玩法
  9. 顶尖数据挖掘辅助教学套件(TipDM-T6)产品白皮书
  10. 通过OC实现简单的冒泡排序
  11. project文件问题
  12. java实现——009Fibonacci数列
  13. Spark RDD算子介绍
  14. curl错误码说明
  15. mybatis 源码分析一
  16. 【aardio】是否取消三个按键的对话框
  17. ORA-00257 archiver error的处理
  18. BZOJ1430小猴打架——prufer序列
  19. css学习_标签的显示模式
  20. BZOJ 3993 [SDOI2015]星际战争 | 网络流 二分答案

热门文章

  1. Oracle记录登录失败的触发器
  2. gcc __attribute__
  3. flask自定义session
  4. java 并发——内置锁
  5. vue过滤器和监视器的小例子
  6. 学会JS的this这一篇就够了
  7. Zookeeper那些事
  8. 2017 Multi-University Training Contest - Team 1 03Colorful Tree
  9. JAVA BIO与NIO、AIO的区别
  10. quartz (一) 基于 Quartz 开发企业级任务调度应用