gpt4 book ai didi

android - 将 MySQL 工作台连接到 Eclipse

转载 作者:行者123 更新时间:2023-11-29 13:10:26 25 4
gpt4 key购买 nike

我对编程非常陌生,以前从未使用过 MySQL,所以请耐心等待!我正在尝试将 MySQL Workbench 连接到我的 Eclipse 项目,但我不知道从哪里开始。有人可以解释一下我需要经历的流程以及我将如何实现它们吗?

谢谢!

最佳答案

在Eclipse中创建一个java项目

创建包名称com.example.sql

在您的项目中包含mysqlconnector.jar

package com.example.sql;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCExample {
public static Connection con;
public static void main(String[] argv) {
try
{
connectionQuery();

PreparedStatement statement = con.prepareStatement("SELECT * from table_name");/*write query inside of prepared statement*/
ResultSet result = statement.executeQuery();
System.out.println("DataBase table accessed");

while(result.next())
{
String retrievedid= result.getString("Column_name");

System.out.println(retrievedid);
}


con.close();
}

catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage().toString());
}
}

public static void connectionQuery()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/boopathi","root","root");
System.out.println("Remote DB connection established");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.out.println("Remote server could not be connected");
}
catch (NullPointerException e)
{
e.printStackTrace();
System.out.println("Remote server could not be connected");
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("Remote db connection establishment error");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("False query");
}
}
}

Refer this link also

关于android - 将 MySQL 工作台连接到 Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22169218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com