Java Tomcat website deployed the second day - database connection
Connecting SQL Server with Tomcat seems to be simple, add SQL Server data sources in the ODBC data source, and then connect to the general ODBC method, this should be an ODBC data source universal method
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; String sConnStr = "jdbc: odbc: databaseName"; String userId = "sa"; String userPass = "3317588"; Connection conn = null; ResultSet rs = null; Class. Forname (sdbdriver) .newinstance (); conn = drivermanager.getConnection (Sconnstr, UserID, Userpass);
It should be noted that class.Forname (sdbdriver) .NewInstance () must be used to capture errors with try, catch (exception), otherwise the incorrect.
Tomcat is connected to the mysql data source, MySQL is free, and the network and Tomcat are more useful, so Tomcat with mysql seems to be necessary, the process is as follows:
1. Install mysql: from
http://dev.mysql.com/get/downloads/mysql-4.0/mysql-4.0.20d-win.zip/from/pick#mirror
Select a mysql installer download under Windows. Here: http://dev.mysql.com/get/downloads/mysql-4.0/mysql-4.0.20d-win.zip/from/http://mysql.linuxForum.net/
Install to C: / MySQL. Run c: /mysql/bin/winmysqladmin.exe enables mysql.ini to take effect while mysqld-nt.exe is also started. To make it easy to use, you can add a C: / mysql / bin path in the Path system environment variable. Run / Enter mysql.exe, open as a cute console management program as SQLPlus.exe in Oracle. Mysql.exe's basic command:
MySQL -UROOT -P root // Log in to the local database with the username root and password root, the default account is root, password space, ie mysql -u root -p, prompt Enter Password, return to the bus
You can create a database after logging in with root.
Create Database DatabaseName; // Create a database
DROP DATABASE DATABASENAME; / / Delete Database
Connect Test; / / The default has a TEST database, here is connected to the Test database
Connect DatabaseName Hostname; // Full syntax
Create Table Test (a int); //, create a simple table
Show tables; // Show all tables all of the current database
Show databases; // Show all databases
2. Mysql Java Tomcat requires third-party driver support, you can
http://dev.mysql.com/get/downloads/connector-j/mysql-connector-java-3.0.14-production.zip/from/pick
Download this driver, download address:
Http://dev.mysql.com/get/downloads/connector-j/mysql-connector-java-3.0.14-production.zip/from/http://mysql.linux process.NET/
Unfold the downloaded file and find mysql-connector-java-3.0.14-production.jar. Copy it to c: / tomcat / common / lib, if you want to call in Tomcat, you must copy it under this path. If you only call it in the Java environment, you can don't forget to add in the ClassPath environment variable. Upper c: /tomcat/common/lib/mysql-connector-java-3.0.14-production.jar
3. Try the driver, create a new JSP try {class.Forname ("org.gjt.mm.mysql.driver). NewInstance ();} catch (exception e) {system.out.println (" exception ");}
If this exception does not produce, the driver is already available, and next is the operation of the connection database. If you have an abnormality, check the above steps.
4. Connect the database, drive the initialization of the initialization, plus the following statement connection conn; statement stmt; result {string url = "jdbc: mysql: // localhost / test? Useunicode = true"; system.out. Println (URL); conn = drivermanager.getConnection (URL); System.out.Println ("getConnection");
Just started using the URL is
JDBC: mysql: //127.0.0.1/test? user = admin&&= admin&& useunicore = true;
I can't even connect, I can't get system.out.println ("getConnection"); this line, it is suspected to have a problem with the connection string, which can look like someone else. The Admin Username is seen in Winmysqladmin.exe.
Attempts in MySQL, Connect Test 192.168.10.80 (My IP) Access Denger, Connect Test LGQ (My Computer Name) Access Refused, using Connect Test Localhost and Connect Test 127.0.0.1, and then discovering all in Connect Without the username and password, then User and Password inside the jointer, it is quite strange, plus a password is still not.
Finally, it is still connected, and the road to the construction of my personal website is nearly one step.