Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

How to access the another system mysql database through java program?

How to access the another system mysql database through java program?Am using the following program but i have get the communication error?what are the changes are need to connect the another system mysql database?

  Public void dbconnection() {

            String name = "";
            String port = "3306";
            String user = "system";
            String pass = "system";
            String dbname = "cascade_demo";
            String host="192.168.1.61";

            try {

                Class.forName("com.mysql.jdbc.Driver");

                  String url = "jdbc:mysql://"+host+":"+  port + "/" + dbname;
                System.out.println("URL:" + url);
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection(url, user, pass);
                String qry2 = "select * from item_master";
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery(qry2);
                while (rs.next()) {

                    name = rs.getString(1);
                    System.out.println("Name:" + name);

                }


                rs.close();
                st.close();
                con.close();


            } catch (Exception e) {
                System.out.println("Exception:" + e);
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not creating an instance of the driver class:

Class.forName("com.mysql.jdbc.Driver").newInstance();

[update: not necessary after all, ignore that]

And you're also referencing "sun.jdbc.odbc.JdbcOdbcDriver", is that necessary? If so, shouldn't you instantiate it also? [update: probably not]

If it works with localhost, and not with the IP specified, you need to configure mysql to listen on all ports.

jcomeau@intrepid:/tmp$ cat dbconnection.java; javac dbconnection.java; sudo java -cp .:/usr/share/maven-repo/mysql/mysql-connector-java/5.1.16/mysql-connector-java-5.1.16.jar  dbconnection
import java.sql.*;
public class dbconnection {
 public static void main(String args[]) {
  String name = "";
  String port = "3306";
  String user = "root";
  String pass = "";
  String dbname = "imagetagging";
  String host="127.0.0.1";
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   String url = "jdbc:mysql://"+host+":"+  port + "/" + dbname;
   System.out.println("URL:" + url);
   //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection(url, user, pass);
   String qry2 = "select * from taggers";
   Statement st = con.createStatement();
   ResultSet rs = st.executeQuery(qry2);
   while (rs.next()) {
    name = rs.getString(1);
    System.out.println("Name:" + name);
   }
   rs.close();
   st.close();
   con.close();
  } catch (Exception e) {
   System.out.println("Exception:" + e);
  }
 }
}
URL:jdbc:mysql://127.0.0.1:3306/imagetagging
Name:1
Name:2
Name:3
Name:4
Name:5
Name:6
Name:7
Name:8
Name:9
Name:10
Name:11
Name:12
Name:13
Name:14
Name:15
Name:16
Name:17
Name:18
Name:19
Name:20
Name:21

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...