简体   繁体   English

如何从Mysql数据库读取

[英]How can I read from Mysql database

Could you help me organize reading from a database. 您能帮我整理一下从数据库中读取的内容吗? I'm under Windows. 我在Windows下。 I get this error message: java.sql.SQLException: No suitable driver found for localhost 我收到此错误消息:java.sql.SQLException:未找到适合本地主机的驱动程序

This is an extract from my code: 这是我的代码的摘录:

public class JavaApplication10 {
    public static void main(String[] args) {
        Connection conn  = null;        
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            Properties connInfo = new Properties();

            connInfo.put("characterEncoding","UTF8");
            connInfo.put("user", "root");
            connInfo.put("password", "goskomstat");

            conn  = DriverManager.getConnection("localhost", connInfo);

Being in Windows command line I can type: C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin>mysql -u root -p 在Windows命令行中,我可以输入:C:\\ Program Files \\ MySQL \\ MySQL Server 5.6 \\ bin> mysql -u root -p

Then I ehter the password 'goskomstat' and can operate my databases. 然后,我输入密码“ goskomstat”并可以操作我的数据库。

Could you give me a hint what to do? 你能给我一个提示怎么办吗?

You need to define your connection as follows: 您需要按以下方式定义连接:

conn = DriverManager.getConnection("jdbc:mysql://localhost/", connInfo);

That allows you to specify the JDBC driver by using the specific URI prefix. 这样,您就可以使用特定的URI前缀来指定JDBC驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM