Home Database Mysql Tutorial jsp连接mysql数据库问题_MySQL

jsp连接mysql数据库问题_MySQL

Jun 01, 2016 pm 01:55 PM
beginner Database Connectivity

  【导读】本文针对初学者介绍关于jsp连接mysql数据库的问题。

  我想对于初学者来说,尝试怎样将jsp与数据库连接起来,那是一件迫切想知道的事情。其实,以后在做网站的时候,都要涉及到与数据库的连接。我在这里连的是Mysql。之所以选择Mysql,因为我喜欢开源的它,与它的平台无关性,和小巧却不失功能性。是一个非常不错的选择。

  首先确定机子上已经安装了Mysql。

  在这之前做的工作是要在Mysql的官方下载它的驱动包,可以免费获得。

  将它拷贝到tomcat的common/lib目录下;(我用的测试服务器是tomcat5.0)

  然后写如下代码,测试是否连接正常。

java.lang.String strConn;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn= java.sql.DriverManager.getConnectio("jdbc:mysql://localhost/test","root","");
%>
  保存为一个jsp文件,然后放到tomcat的G:Apache Software FoundationTomcat 5.0webappsROOT目录下,然后在浏览器里查看此页面,如果不报异常,显示空白页面,则表示已经正确连接上了.接下来的工作,就要测试连接mysql了.

  我们完全可以把所有的代码写进一个类里面.如下代码所示:

public class As
{
public static void main(String[] args)
{
String str=null; //
Statement stmt=null;
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
//加载驱动程序
String url="jdbc:mysql://localhost:3306/qqnumber";
String user="root";
String password="******";
String sql="select * from qqNumber"; //
conn=DriverManager.getConnection(url,user,password); //建立连接
stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery(sql); //获得数据结果集合 ResultSet 接口
//提供对数据表的访问。ResultSet 对象通常是通过执行“语句”来生成的
rs.next();
str=rs.getString("number");
System.out.println(str);
System.out.println("数据库操作成功,恭喜你");
rs.close();
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException e)
{
System.out.println(e);
}
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
System.out.println(e);
}
}
}//finally
}//main()
}
  我第一次调试遇到的错误.

  com.mysql.jdbc.UpdatableResultSet@1cb25f1数据库操作成功,恭喜你 ,看后面这一句,很明显,这已经成功运行了!可是我要显示的数据查询结果,并没有显示出来.

  问题在于ResultSet rs=stmt.executeQuery(sql); 所获得的数据结果集合的问题.我们要想显示出来必须将它转化为字符串的形式.

  出现下面这一种问题java.sql.SQLException: Before start of result set的原因是:ResultSet 始终有一个游标指向其当前数据行。最初,游标定位在第一行的前面。next()方法将游标移至下一行。当忘记写next方法时,就会报这样的错误.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Become a C expert: Five must-have compilers recommended Become a C expert: Five must-have compilers recommended Feb 19, 2024 pm 01:03 PM

From Beginner to Expert: Five Essential C Compiler Recommendations With the development of computer science, more and more people are interested in programming languages. As a high-level language widely used in system-level programming, C language has always been loved by programmers. In order to write efficient and stable code, it is important to choose a C language compiler that suits you. This article will introduce five essential C language compilers for beginners and experts to choose from. GCCGCC, the GNU compiler collection, is one of the most commonly used C language compilers

C++ or Python, which one is more suitable for beginners? C++ or Python, which one is more suitable for beginners? Mar 25, 2024 am 10:54 AM

C++ or Python, which one is more suitable for beginners? In this era of information technology sweeping the world, programming ability has become an essential skill. In the process of learning programming, choosing a suitable programming language is particularly important. Among many programming languages, C++ and Python are two popular choices for beginners. So, which one is more suitable for beginners, C++ or Python? The following will compare the advantages and disadvantages of the two in various aspects, and why choosing a certain language is more helpful for beginners to get started with programming.

Advanced PHP database connections: transactions, locks, and concurrency control Advanced PHP database connections: transactions, locks, and concurrency control Jun 01, 2024 am 11:43 AM

Advanced PHP database connections involve transactions, locks, and concurrency control to ensure data integrity and avoid errors. A transaction is an atomic unit of a set of operations, managed through the beginTransaction(), commit(), and rollback() methods. Locks prevent simultaneous access to data via PDO::LOCK_SHARED and PDO::LOCK_EXCLUSIVE. Concurrency control coordinates access to multiple transactions through MySQL isolation levels (read uncommitted, read committed, repeatable read, serialized). In practical applications, transactions, locks and concurrency control are used for product inventory management on shopping websites to ensure data integrity and avoid inventory problems.

Pandas Beginner's Guide: HTML Table Data Reading Tips Pandas Beginner's Guide: HTML Table Data Reading Tips Jan 09, 2024 am 08:10 AM

Beginner's Guide: How to Read HTML Tabular Data with Pandas Introduction: Pandas is a powerful Python library for data processing and analysis. It provides flexible data structures and data analysis tools, making data processing simpler and more efficient. Pandas can not only process data in CSV, Excel and other formats, but can also directly read HTML table data. This article will introduce how to use the Pandas library to read HTML table data, and provide specific code examples to help beginners

How to configure database connection in mybatis How to configure database connection in mybatis Jan 15, 2024 pm 02:12 PM

How to configure database connection in mybatis: 1. Specify the data source; 2. Configure the transaction manager; 3. Configure the type processor and mapper; 4. Use environment elements; 5. Configure aliases. Detailed introduction: 1. Specify the data source. In the "mybatis-config.xml" file, you need to configure the data source. The data source is an interface, which provides a database connection; 2. Configure the transaction manager to ensure the normality of database transactions. For processing, you also need to configure the transaction manager; 3. Configure the type processor and mapper, etc.

Why does my PHP database connection fail? Why does my PHP database connection fail? Jun 05, 2024 pm 07:55 PM

Reasons for a PHP database connection failure include: the database server is not running, incorrect hostname or port, incorrect database credentials, or lack of appropriate permissions. Solutions include: starting the server, checking the hostname and port, verifying credentials, modifying permissions, and adjusting firewall settings.

A must-read for beginners: How to choose the appropriate Django version according to your needs? A must-read for beginners: How to choose the appropriate Django version according to your needs? Jan 19, 2024 am 08:20 AM

For beginners, choosing the appropriate Django version is an important and must-face issue. As an efficient web framework, Django has a large number of users and developers, so it also has multiple versions to meet the needs of different products and applications. But how do you choose the right Django version based on your project needs? Below we will use some examples to help you choose the version that suits you. Confirm that the database used Django supports multiple databases, including MySQL, Postgre

Python beginners must learn: Master the basic usage of lambda functions Python beginners must learn: Master the basic usage of lambda functions Feb 02, 2024 pm 06:41 PM

Essential for beginners: To master the basic usage of lambda functions in Python, specific code examples are required. Overview: Python is a simple and easy-to-learn programming language. It has attracted the love of many programmers with its concise and flexible syntax. In Python, a lambda function is a special anonymous function that can be defined directly where the function is required without giving it a name. This article will introduce the basic use of lambda functions and provide specific code examples to help beginners better understand

See all articles