Java工程连接数据库

2019-07-22  本文已影响0人  小鱼儿_f32a

package com.sinosoft.test;

import java.sql.*;

public class DBTest {

//mysql驱动包名

    private static final StringDRIVER_NAME ="com.mysql.jdbc.Driver";

//数据库连接地址

    private static final StringURL ="jdbc:mysql://IP(具体IP):3306/数据库名()?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull";

//用户名

    private static final StringUSER_NAME ="数据库账户名";

//密码

    private static final StringPASSWORD ="密码";

public static void main(String[] args) {

Connection connection =null;

try {

//加载mysql的驱动类

            Class.forName(DRIVER_NAME);

//获取数据库连接

            connection = DriverManager.getConnection(URL,USER_NAME,PASSWORD);

//mysql查询语句

            String sql ="SELECT productname FROM lrproduct";

PreparedStatement prst = connection.prepareStatement(sql);

//结果集

            ResultSet rs = prst.executeQuery();

while (rs.next()) {

System.out.println("用户名:" + rs.getString("productname"));

}

rs.close();

prst.close();

}catch (Exception e) {

e.printStackTrace();

}finally {

if (connection !=null) {

try {

connection.close();

}catch (SQLException e) {

e.printStackTrace();

}

}

}

    }

}

jdbc

https://blog.csdn.net/u012075383/article/details/78254482

https://blog.csdn.net/u014631593/article/details/52791391

上一篇下一篇

猜你喜欢

热点阅读